简体   繁体   English

映射中的返回Object.keys未定义

[英]return Object.keys in a map got undefined

What's wrong with my loop below? 我下面的循环出了什么问题? I want to calculate the total_count and return [total, total] after some manipulation within a map, but I got undefined. 我想计算total_count并在映射内进行一些操作后返回[total,total],但是我不确定。

my raw data 我的raw数据

const raw = [{
  "device_info": {
    "name": "cam1",
  },
  "age_range": {
    "0-10": {
      "age_range": "0-10",
      "total_count": 15,
      "male_count": 6,
      "female_count": 9
    },
    "11-20": {
      "age_range": "11-20",
      "total_count": 11,
      "male_count": 7,
      "female_count": 4
    },
    "21-30": {
      "age_range": "21-30",
      "total_count": 922,
      "male_count": 452,
      "female_count": 470
    }
  }
}, {
  "device_info": {
    "name": "cam2",
  },
  "age_range": {
    "0-10": {
      "age_range": "0-10",
      "total_count": 1,
      "male_count": 1,
      "female_count": 0
    },
    "11-20": {
      "age_range": "11-20",
      "total_count": 2,
      "male_count": 0,
      "female_count": 2
    },
    "21-30": {
      "age_range": "21-30",
      "total_count": 90,
      "male_count": 58,
      "female_count": 32
    }
  }
}]

Loop

const x = raw.map(obj => {
  return Object.keys(obj).forEach(key => {
    let total = 0
    if (key === 'age_range') {
      total = Object.keys(obj.age_range).reduce((acum, innerKey) => {
        return acum + obj.age_range[innerKey].total_count
      }, 0)
      console.log(total)
    }
  });
})

console.log('x', x)

https://jsfiddle.net/19m3f7fs/1 https://jsfiddle.net/19m3f7fs/1

Array#forEach doesn't return anything, use Array#map and Object.values instead: Array#forEach不返回任何内容,而是使用Array#mapObject.values

const x = raw.map(obj => {
  return Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0)
})

Working example: 工作示例:

 const raw = [{ "device_info": { "name": "cam1", }, "age_range": { "0-10": { "age_range": "0-10", "total_count": 15, "male_count": 6, "female_count": 9 }, "11-20": { "age_range": "11-20", "total_count": 11, "male_count": 7, "female_count": 4 }, "21-30": { "age_range": "21-30", "total_count": 922, "male_count": 452, "female_count": 470 } } }, { "device_info": { "name": "cam2", }, "age_range": { "0-10": { "age_range": "0-10", "total_count": 1, "male_count": 1, "female_count": 0 }, "11-20": { "age_range": "11-20", "total_count": 2, "male_count": 0, "female_count": 2 }, "21-30": { "age_range": "21-30", "total_count": 90, "male_count": 58, "female_count": 32 } } }] const x = raw.map(obj => Object.values(obj.age_range).reduce((acc, item) => acc + item.total_count, 0)) console.log(x) 

Your problem is, that the raw.map returns whatever comes from Object.key(obj).forEach(...) 您的问题是,raw.map返回来自Object.key(obj).forEach(...)的任何内容

But for each is just an "iterator" function. 但是对于每个仅仅是“迭代器”功能。 It actually doesnt return anything. 它实际上不返回任何东西。

See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach 请参阅: https//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Change like this .define the total in first map varible and remove the return from forEach .ForEach not returning anything .And add the return satement only inside the first map. 像这样更改。在第一个map变量中定义总数,并从forEach删除收益。ForEach不返回任何东西。仅在第一个地图内添加收益。

 const raw = [{ "device_info": { "name": "cam1", }, "age_range": { "0-10": { "age_range": "0-10", "total_count": 15, "male_count": 6, "female_count": 9 }, "11-20": { "age_range": "11-20", "total_count": 11, "male_count": 7, "female_count": 4 }, "21-30": { "age_range": "21-30", "total_count": 922, "male_count": 452, "female_count": 470 } } }, { "device_info": { "name": "cam2", }, "age_range": { "0-10": { "age_range": "0-10", "total_count": 1, "male_count": 1, "female_count": 0 }, "11-20": { "age_range": "11-20", "total_count": 2, "male_count": 0, "female_count": 2 }, "21-30": { "age_range": "21-30", "total_count": 90, "male_count": 58, "female_count": 32 } } }] const x = raw.map(obj => { let total = 0 Object.keys(obj).forEach(key => { if (key === 'age_range') { total = Object.keys(obj.age_range).reduce((acum, innerKey) => { return acum + obj.age_range[innerKey].total_count }, 0) } }); return total }) console.log('x', x) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM