lodash - project/transform object into key value array

前端 未结 6 700
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 13:10

I\'m about to use forOwn to iterate through an object\'s properties and create an array manually and can\'t helping thinking there\'s a oneliner already availab

6条回答
  •  粉色の甜心
    2020-12-05 14:08

    If you are using lodash/fp you can use _.entries

    const a = { one: 123, two: { value: 'b' }};
    
    const pairs = _.entries(a).map(p => ({ key:p[0], value: p[1] }))
    
    console.log(pairs)
    // [
    //   {
    //     "key": "one",
    //     "value": 123
    //   },
    //   {
    //     "key": "two",
    //     "value": {
    //       "value": "b"
    //     }
    //   }
    // ]
    
    

提交回复
热议问题