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 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题