Angular2,创建新的数组/贴图 [英] Angular2, create new array/map
本文介绍了Angular2,创建新的数组/贴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有json数组-测试"其中一个键是"GroupID"我希望获取所有的"GroupID"并创建包含它的新数组(如果可能的话,则为地图-请勿重复)我尝试了以下操作,但是groupID仅包含1个值我尝试了.push,[]和其他多种方式,但总是遇到错误我该怎么办?
I have json array - "tests" inside one of the keys is "GroupID" I wish to take all the "GroupID" and create new array that will include it (if possible as map - so no duplicated) I tried the following, but groupID contains only 1 value I tried .push, [], and many other ways and always getting errors how can I do it?
...
groupID: any[];
...
for (var index = 0; index < this.tests.length; index++) {
var element = this.tests[index];
this.groupID = element.GroupID;
}
console.log(this.groupID);
推荐答案
要获取数组,请替换
groupID: any[];
使用
groupID: any[] = [];
然后,也替换
this.groupID = element.GroupID;
使用
this.groupID.push(element.GroupID);
这篇关于Angular2,创建新的数组/贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文