gpt4 book ai didi

javascript - jQuery:打印嵌套数组中多个键的所有值

转载 作者:行者123 更新时间:2023-12-03 08:11:51 25 4
gpt4 key购买 nike

我在 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/相应打印属于“name”的“id”和“category”的值

理想结果:
设备id=0
类别:相机
名称:600D套件

设备 ID = 1
类别:相机
名称:600D套件

设备 ID = 2
类别:相机
名称:7D套件

这是我的尝试:

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);

如果 return e.name;<,我可以打印数组中“names”的所有值,并打印数组第一项的“id”、“category”和“name”/。但是,一旦我添加了 return e.name == EquipmentName; ,我就收到了“equipment

”的错误

最佳答案

此代码会将所有元素显示到控制台。

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
});

关于javascript - jQuery:打印嵌套数组中多个键的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com