我按照test列表中循环attribution中的元素,正确应该如下图,但是我打印出来是这样的,请问原因是什么?
const test = [
{
serial: '28955c9d501c7ece',
deviceInfo: {
status: true,
country: 'Singapore',
city: 'Singapore',
hostingVendor: '办公室节点',
os: 'android',
providerUuid: '5430826A-171F-53B4-8A64-46F663490B76'
},
attribution: { mac: 0.5, agent: 0.5 },
max: -1,
slaInfo: {
device_sla: 0.984491,
device_platform_sla: 0.899931,
device_device_sla: 0.99
}
},
{
serial: '1ce45ca840047ece',
deviceInfo: {
status: true,
country: 'Singapore',
city: 'Singapore',
hostingVendor: '办公室节点',
os: 'android',
providerUuid: '5430826A-171F-53B4-8A64-46F663490B76'
},
attribution: { mac: 0.5, agent: 0.5 },
max: -1,
slaInfo: {
device_sla: 0.984491,
device_platform_sla: 0.899965,
device_device_sla: 0.99
}
}
]
let cityMap = {
android: {},
ios: {}
};
let ioscount = 0;
let androidcount = 0;
let notRecoveredCnt = 0;
let totalrate = 0;
for (let i = 0; i < test.length; i++) {
var city = test[i].deviceInfo.city;
var os = test[i].deviceInfo.os;
// console.log('zzcity',city)
for (let key in test[i].attribution) {
if (test[i].attribution[key] > 0 && !cityMap[os][city]) {
cityMap[os][city] = {
reason: key,
city: city,
androidcount: androidcount = os == "android" ? 1 : 0,
ioscount: ioscount = os == "ios" ? 1 : 0,
// [os + 'count']: 1,
totalrate: test[i].attribution[key],
notRecoveredCnt: test[i]["deviceInfo"].status == 0 ? 1 : 0,
}
} else if (test[i].attribution[key] > 0) {
cityMap[os][city] = {
reason: key,
city: city,
androidcount: androidcount = os == "android" ? cityMap[os][city].androidcount + 1 : 0,
ioscount: androidcount = os == "ios" ? cityMap[os][city].ioscount + 1 : 0,
// [os + 'count']: (cityMap[city][os + 'count'] ?? 0) + 1,
totalrate: cityMap[os][city].totalrate += test[i].attribution[key],
notRecoveredCnt: cityMap[os][city].notRecoveredCnt + (test[i]["deviceInfo"].status == 0 ? 1 : 0),
}
}
}
}
console.log("zzcityMap", cityMap);