javascripts

我按照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);

img

img

cityMap[os][city] -- > cityMap[os][city + '_' + key]

你没有按照key区分


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 + '_' + key]) {
            cityMap[os][city + '_' + key] = {
                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 + '_' + key] = {
                reason: key,
                city: city,
                androidcount: androidcount = os == "android" ? cityMap[os][city + '_' + key].androidcount + 1 : 0,
                ioscount: androidcount = os == "ios" ? cityMap[os][city + '_' + key].ioscount + 1 : 0,
                // [os + 'count']: (cityMap[city][os + 'count'] ?? 0) + 1,
                totalrate: cityMap[os][city + '_' + key].totalrate += test[i].attribution[key],
                notRecoveredCnt: cityMap[os][city + '_' + key].notRecoveredCnt + (test[i]["deviceInfo"].status == 0 ? 1 : 0),
            }
        }
    }
}
console.log("zzcityMap", cityMap);

这个用浏览器内置的js调试器调试下

原因是在第二个else if语句中,赋值ioscount的语句写错了,应该是赋值给ioscount,而不是又赋值给了androidcount。正确的语句应该是:
ioscount = os == "ios" ? cityMap[os][city].ioscount + 1 : 0,

  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:JavaScripts