我在json
中有一个嵌套数组,如下所示:
var json.result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"}]
这就是我要做的事情:
1 /查找数组中的每个“名称”。
2 /打印相应属于“名称”的“id”和“category”的值
理想的结果:
设备id = 0
类别:相机
名称:600D Kit
设备ID = 1
类别:相机
名称:600D Kit
设备ID = 2
类别:相机
名称:7D Kit
这是我的尝试:
var equipmentName = [];
var listLength = json.result.length;
while (listLength--){
equipmentName[listLength] = json.result[listLength].name
}
console.log(equipmentName);
var equipment = json.result.find(function(e){
return e.name == equipmentName;
});
console.log("equipment id: " + equipment.id);
console.log("category: " +equipment.category);
console.log("name: " +equipment.name);
我可以在数组中打印“names”的所有值,并打印return e.name;
数组中第一项的“id”,“category”和“name”。但是,一旦我添加return e.name == equipmentName;
,我就会收到“设备
答案 0 :(得分:1)
此代码会将所有元素显示到控制台。
var result= [
{"id":"0","category":"Camera","name":"600D Kit", "condition":"OK"},
{"id":"1","category":"Camera","name":"600D Kit", "condition":"missing cap"},
{"id":"2","category":"Camera","name":"7D Kit", "condition":"OK"}
];
result.map(function(element){
console.log(element.id,element.category,element.name);
// Element display functions here
});