我有一个json数组- " groupID“里面的一个键是"GroupID”我希望带上所有的“groupID”并创建新的数组来包含它(如果可能的话作为映射-所以没有重复的)我尝试了以下方法,但groupID只包含1个值我尝试了.push,[]和许多其他方法,但总是得到错误我该怎么做呢?
...
groupID: any[];
...
for (var index = 0; index < this.tests.length; index++) {
var element = this.tests[index];
this.groupID = element.GroupID;
}
console.log(this.groupID);
发布于 2016-09-06 22:59:40
要获取数组,请替换
groupID: any[];使用
groupID: any[] = [];然后,还要替换
this.groupID = element.GroupID;使用
this.groupID.push(element.GroupID);发布于 2016-09-07 03:11:11
多亏了beetleJuice才解决了问题
仅当数组中不存在时才添加:
...
groupID: any[] = [];
...
for (var index = 0; index < this.tests.length; index++) {
var element = this.tests[index];
if (this.groupID.indexOf(element.GroupID) == -1 ){
this.groupID.push(element.GroupID);
}
}
https://stackoverflow.com/questions/39352000
复制相似问题