transform object to array with lodash

后端 未结 11 1469
旧时难觅i
旧时难觅i 2020-12-22 23:38

How can I transform a big object to array with lodash?

var obj = {
  22: {name:\"John\", id:22, friends:[5,31,55], works:{books:[],         


        
11条回答
  •  旧时难觅i
    2020-12-22 23:41

    2017 update: Object.values, lodash values and toArray do it. And to preserve keys map and spread operator play nice:

    // import { toArray, map } from 'lodash'
    const map = _.map
    
    const input = {
      key: {
        value: 'value'
      }
    }
    
    const output = map(input, (value, key) => ({
      key,
      ...value
    }))
    
    console.log(output)
    // >> [{key: 'key', value: 'value'}])

提交回复
热议问题