Jquery 将 JSON 列表的 某个属性值,添加到数组中

发布时间 2023-09-14 17:25:20作者: VipSoft

如果你有一个JSON列表,并且想要将每个对象的某个属性值添加到数组中,你可以使用jQuery的$.each()函数来遍历JSON列表,并获取所需的属性值。以下是一个示例代码:

var jsonList = [  
    { "name": "John", "age": 30, "city": "New York" },  
    { "name": "Jane", "age": 25, "city": "Los Angeles" },  
    { "name": "Bob", "age": 40, "city": "Chicago" }  
];  
  
var array = [];  
  
$.each(jsonList, function(index, item) {  
    array.push(item.name); // 将每个对象的"name"属性值添加到数组中  
});  
  
console.log(array); // ["John", "Jane", "Bob"]

在这个示例中,我们有一个名为jsonList的JSON列表,包含了几个对象。我们使用$.each()函数遍历这个列表,并通过item.name获取每个对象的"name"属性值,然后将其添加到array数组中。最后,我们打印出数组来验证结果。