gpt4 book ai didi

javascript - Javascript 和 AngularJs 中的通用 foreach 循环

转载 作者:行者123 更新时间:2023-11-28 13:15:01 26 4
gpt4 key购买 nike

我陷入了 foreach 循环,我不知道列名称,所以我不能这样做value.Job ,我需要显示带有特定数据的所有列

angular.forEach(data, function(value, key) {
console.log( 'the Object N° ' + i );
'maybe another loop for all the column
console.log( 'the column name is + ': ' + value ??);
});

结果应该是这样的:

the  Object N° 1 
the column name is Name : Matt
the column name is Job : Teacher

the Object N° 2
the column name is Name : Alice
the column name is Job : Doctor

这里有任何想法!

我想查看 JavaScript 和 AngularJs forEach 的选项。

最佳答案

是的,您可以使用 for...in 循环来枚举对象中的所有键,如下所示

var data = [
{Name: "Matt", Job: "Teacher"},
{Name: "Alice", Job: "Doctor"}];

data.forEach(function(e,i){
console.log(i);
for(var key in e){
console.log(key + ':' + e[key] );
}
});

请注意,我使用了 Array.forEach ,但是angular.forEach将具有大致相同的结构。

关于javascript - Javascript 和 AngularJs 中的通用 foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266934/

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