返回地图中的Object.keys未定义

时间:2017-08-18 09:09:31

标签: javascript ecmascript-6

我的循环下面有什么问题?我想计算total_count并在地图中进行一些操作后返回[total,total],但是我得到了未定义。

我的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
    }
  }
}]

循环

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

3 个答案:

答案 0 :(得分:2)

Array#forEach不会返回任何内容,请改为使用Array#mapObject.values

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

工作示例:

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)

答案 1 :(得分:0)

你的问题是,raw.map返回来自Object.key(obj)的任何内容.forEach(...)

但是每个只是一个“迭代器”功能。它实际上没有返回任何东西。

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

答案 2 :(得分:0)

像这样更改。在第一个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)