kicortior 2023-03-17 18:10 采纳率: 50%
浏览 58
已结题

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

  • 写回答

4条回答 默认 最新

  • 文盲老顾 WEB应用领新星创作者 2023-03-17 20:47
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月25日
  • 已采纳回答 3月17日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败