reduce範例(筆記)

  • 83
  • 0
  • ES6
  • 2018-08-27

reduce範例

引用:https://codepen.io/anon/pen/gjraPG?editors=1011

const ary = [{a: 1}, {b: 2}, {c: 3}]
let ans = {keys:[],values:[]}
ans.total = ary.reduce((prev, next) => {
  ans.keys.push(Object.keys(next)[0])
  ans.values.push(next[Object.keys(next)[0]])
  return prev + next[Object.keys(next)[0]]
} ,0)
console.log(ans)