gpt4 book ai didi

javascript - Angular : iterate array with key value pairs

转载 作者:行者123 更新时间:2023-11-30 12:14:31 30 4
gpt4 key购买 nike

我正在创建一个如下所示的数组:

var arr =[];
arr['key1'] = 'value1';
arr['key2'] = 'value2';

如果在 ng-repeat 标签中使用这个数组,它不会显示任何东西。有什么办法让它发挥作用吗?

<div data-ng-repeat='(key,value) in arr'>{{key}} - {{value}}</div>

有什么办法可以让它发挥作用吗?

最佳答案

要走的路,是创建普通对象(而不是数组)

// instead of creatin of an Array
// $scope.myArr = [];
// we just create plain object
$scope.myArr = {};
...
// here we just add properties (key/value)
$scope.myArr['attempt1'] = {};
...
// which is in this case same as this syntax
$scope.myArr.attempt1 = {};

你是 updated plunker

例如,在此处查看更多详细信息:

Javascript: Understanding Objects vs Arrays and When to Use Them. [Part 1]

关于javascript - Angular : iterate array with key value pairs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32796881/

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