gpt4 book ai didi

javascript - Angular如何在json数组中显示键

转载 作者:行者123 更新时间:2023-11-28 11:53:25 24 4
gpt4 key购买 nike

我有以下 JSON 对象数组:

"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]

我想获取 JSON 数组中的特定键:

for (var ke in employees) {
if (employees.hasOwnProperty(ke)) {
console.log(employees[ke].lastName);
};

但是它不起作用!!

最佳答案

您在 for 循环末尾缺少一个右大括号,它应该是:

for (var ke in employees) {
if (employees.hasOwnProperty(ke)) {
console.log(employees[ke].lastName);
};
}

Demo

但是,您可以使用 angular.forEach相反,像这样:

angular.forEach(employees, function (ke) {
if(ke.lastName) {
console.log(ke.lastName);
}
});

Demo

关于javascript - Angular如何在json数组中显示键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206890/

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