transform object to array with lodash

后端 未结 11 1474
旧时难觅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条回答
  •  半阙折子戏
    2020-12-23 00:07

    There are quite a few ways to get the result you are after. Lets break them in categories:

    ES6 Values only:

    Main method for this is Object.values. But using Object.keys and Array.map you could as well get to the expected result:

    Object.values(obj)
    Object.keys(obj).map(k => obj[k])
    

    var obj = {
      A: {
        name: "John"
      },
      B: {
        name: "Ivan"
      }
    }
    
    console.log('Object.values:', Object.values(obj))
    console.log('Object.keys:', Object.keys(obj).map(k => obj[k]))

    ES6 Key & Value:

    Using map and ES6 dynamic/computed properties and destructuring you can retain the key and return an object from the map.

    Object.keys(obj).map(k => ({[k]: obj[k]}))
    Object.entries(obj).map(([k,v]) => ({[k]:v}))
    

    var obj = {
      A: {
        name: "John"
      },
      B: {
        name: "Ivan"
      }
    }
    
    console.log('Object.keys:', Object.keys(obj).map(k => ({
      [k]: obj[k]
    })))
    console.log('Object.entries:', Object.entries(obj).map(([k, v]) => ({
      [k]: v
    })))

    Lodash Values only:

    The method designed for this is _.values however there are "shortcuts" like _.map and the utility method _.toArray which would also return an array containing only the values from the object. You could also _.map though the _.keys and get the values from the object by using the obj[key] notation.

    Note: _.map when passed an object would use its baseMap handler which is basically forEach on the object properties.

    _.values(obj)
    _.map(obj)
    _.toArray(obj)
    _.map(_.keys(obj), k => obj[k])
    

    var obj = {
      A: {
        name: "John"
      },
      B: {
        name: "Ivan"
      }
    }
    
    console.log('values:', _.values(obj))
    console.log('map:', _.map(obj))
    console.log('toArray:', _.toArray(obj))
    console.log('keys:', _.map(_.keys(obj), k => obj[k]))

    Lodash Key & Value:

    // Outputs an array with [[KEY, VALUE]]
    _.entries(obj)
    _.toPairs(obj)
    
    // Outputs array with objects containing the keys and values
    _.map(_.entries(obj), ([k,v]) => ({[k]:v}))
    _.map(_.keys(obj), k => ({[k]: obj[k]}))
    _.transform(obj, (r,c,k) => r.push({[k]:c}), [])
    _.reduce(obj, (r,c,k) => (r.push({[k]:c}), r), [])
    

    var obj = {
      A: {
        name: "John"
      },
      B: {
        name: "Ivan"
      }
    }
    
    // Outputs an array with [KEY, VALUE]
    console.log('entries:', _.entries(obj))
    console.log('toPairs:', _.toPairs(obj))
    
    // Outputs array with objects containing the keys and values
    console.log('entries:', _.map(_.entries(obj), ([k, v]) => ({
      [k]: v
    })))
    console.log('keys:', _.map(_.keys(obj), k => ({
      [k]: obj[k]
    })))
    console.log('transform:', _.transform(obj, (r, c, k) => r.push({
      [k]: c
    }), []))
    console.log('reduce:', _.reduce(obj, (r, c, k) => (r.push({
      [k]: c
    }), r), []))

    Note that in the above examples ES6 is used (arrow functions and dynamic properties). You can use lodash _.fromPairs and other methods to compose an object if ES6 is an issue.

提交回复
热议问题