【发布时间】:2019-01-21 14:14:13
【问题描述】:
目的是拥有一个数组,其中包含来自另一个数组的特定键的所有唯一值。这些唯一值(现在是新数组中的键)将具有它们在原始数组中出现的次数作为值。
这是我目前的代码:
for (let i = 0; i < data.length; i++) {
let theStupidKey = data[i].DeliveryStatus;
if (
this.differentValuesOfStatus.indexOf(theStupidKey) == "-1"
) {
this.differentValuesOfStatus.push(theStupidKey: 1);
// this.differentValuesOfStatus[theStupidKey].push(theStupidKey = 1);
}
else {
this.differentValuesOfStatus[theStupidKey] = 1
}
}
console.log(this.differentValuesOfStatus);
};
但是语法是错误的,我尝试了所有我能想到的方法来让它工作。
基本上我是在遍历一个数组。
如果第二个数组中不存在“deliverystatus”键的值,我将其添加为“1”。
如果它已经存在,我想添加另一个数字。
【问题讨论】:
标签: javascript arrays json angular typescript