文章目录
- 需求
- 分析
需求
已知返回的数据如下:,现需找到指定字段的那一项并输出
分析
写一个方法就能搞定,代码中定义了一个名为 findDeviceByName 的函数,它会遍历给定的数组,查找其中 deviceName 字段值等于目标名称的数据,并返回该条数据。然后根据返回结果打印输出或进行其他操作。
function findDeviceByName(data, deviceName) {for (let i = 0; i < data.length; i++) {if (data[i].deviceName === deviceName) {return data[i];}}return null;
}// 假设data是你提供的数组
let data = [{"id": "1","company": "基康(北京)","type": "BGK4911-25","product": "钢筋计","deviceName": "振弦式_钢筋计1","codeName": "GJJ","tableName": "sensor_data_GJJ","hydrographName": "应力、温度过程线图","labelY": "应力","dataTypeY": "3","reverseOrderY": "true","labelYC": "温度","dataTypeYC": "3","reverseOrderYC": "true","textualCriticismList": [],"physicalQuantityList": []},// ... (其余数据)
];let result = findDeviceByName(data, "1");
if (result) {console.log(result);
} else {console.log("未找到符合条件的数据");
}