gpt4 book ai didi

angularjs - Angular JS - angular.forEach - 如何获取对象的键?

转载 作者:行者123 更新时间:2023-12-02 19:27:55 24 4
gpt4 key购买 nike

我有如下所示的 JSON 对象

{
"txt_inc_Application": {
"EWS": true,
"EWindow": true
},
"txt_inc_IncidentType": {
"Brand Damage": true,
"Internal failure": true
}
}

我正在使用 angular.forEach 来获取值

$scope.filterFormula=function() {
angular.forEach($scope.filters, function(filterObj , filterIndex) {
angular.forEach(filterObj, function(value , key) {
console.log(value+"--"+key)
})
})
}

如何在循环中获取“txt_inc_Application”和“txt_inc_IncidentType”?

另外,当像下面这样在 html 中调用 Angular 函数时,为什么它会被执行两次?

{{filterFormula()}}

enter image description here

最佳答案

forEach 中迭代器的第一个参数是值,第二个参数是对象的键。

angular.forEach(objectToIterate, function(value, key) {
/* do something for all key: value pairs */
});

在您的示例中,外部 forEach 实际上是:

angular.forEach($scope.filters, function(filterObj , filterKey)

关于angularjs - Angular JS - angular.forEach - 如何获取对象的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18396279/

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