javascript 使用下划线 js 或 lodash 将对象解析为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33886413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:00:09  来源:igfitidea点击:

Parsing object to array using underscore js or lodash

javascriptunderscore.jslodash

提问by Dibish

I have a requirement to parse an object and convert in to an array using Underscore/Lo-Dash

我需要使用 Underscore/Lo-Dash 解析对象并转换为数组

Tried using usersocre, but its not getting expected result. Sorry am new to underscore js. Your help is much appreciated.

尝试使用 userocre,但没有得到预期的结果。对不起,我不熟悉下划线 js。非常感谢您的帮助。

var arr = _.values(obj)

var obj = {
    '2c13790be20a06a35da629c71e548afexing': [{
        connector: '',
        displayName: 'John',
        loginName: '',
        userImage: '2.jpg'
    }],
    '493353a4c5f0aa71508d4055483ff979linkedinpage': [{
        connector: '',
        displayName: 'Mak',
        loginName: '',
        userImage: '1.jpg'
    }]
}

expected output array

预期输出数组

array = [{
    connector: '2c13790be20a06a35da629c71e548afexing',
    displayName: 'John',
    loginName: '',
    userImage: '2.jpg'
}, {
    connector: '493353a4c5f0aa71508d4055483ff979linkedinpage',
    displayName: 'Mak',
    loginName: '',
    userImage: '1.jpg'
}]

采纳答案by Tushar

Use Object.keyswith forEachand add object in the array.

使用Object.keyswithforEach并在数组中添加对象。

  1. Get all the keys of the object
  2. Iterate over the keys array using forEach
  3. Push first element of the subarray in the result array.
  1. 获取对象的所有键
  2. 使用迭代键数组 forEach
  3. 将子数组的第一个元素推送到结果数组中。

Code:

代码:

var arr = [];
Object.keys(obj).forEach(function(e) {
    // Get the key and assign it to `connector`
    obj[e][0].connector = e;
    arr.push(obj[e][0]);
});

var obj = {
  '2c13790be20a06a35da629c71e548afexing': [{
    connector: '',
    displayName: 'John',
    loginName: '',
    userImage: '2.jpg'
  }],
  '493353a4c5f0aa71508d4055483ff979linkedinpage': [{
    connector: '',
    displayName: 'Mak',
    loginName: '',
    userImage: '1.jpg'
  }]
};

var arr = [];
Object.keys(obj).forEach(function(e) {
  obj[e][0].connector = e;
  arr.push(obj[e][0]);
});

console.log(arr);
document.getElementById('output').innerHTML = JSON.stringify(arr, 0, 4);
<pre id="output"></pre>



The same code be converted to use Lodash/Underscore's forEach.

相同的代码被转换为使用 Lodash/Underscore 的forEach.

var arr = [];
_.forEach(obj, function(e, k) {
    e[0].connector = k;
    arr.push(e[0]);
});

var obj = {
  '2c13790be20a06a35da629c71e548afexing': [{
    connector: '',
    displayName: 'John',
    loginName: '',
    userImage: '2.jpg'
  }],
  '493353a4c5f0aa71508d4055483ff979linkedinpage': [{
    connector: '',
    displayName: 'Mak',
    loginName: '',
    userImage: '1.jpg'
  }]
};

var arr = [];

_.forEach(obj, function(e, k) {
  e[0].connector = k;
  arr.push(e[0]);
});

console.log(arr);
document.getElementById('output').innerHTML = JSON.stringify(arr, 0, 4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
<pre id="output"></pre>

回答by Manwal

You can go like this in Underscore:

你可以在Underscore 中这样做:

_(obj).each(function(elem, key){
   arr.push(elem[0]);
});

See it in action

看到它在行动

回答by Quy

If you're talking about functional, I usually don't want to alter the original object in any way, so I use extend to create a new object. I overwrite the connector property in the next line and return the object.

如果你说的是函数式,我通常不想以任何方式改变原始对象,所以我使用扩展来创建一个新对象。我在下一行覆盖连接器属性并返回对象。

_.map(function( arr, key ) {
  var obj = _.extend({}, arr.pop());
  obj.connector = key;
  return obj;
});

回答by Malk

Grab the object from the first value in each key's array and assign the key to the connector property. No library needed.

从每个键的数组中的第一个值中获取对象,并将键分配给连接器属性。不需要图书馆。

var result = [];
for (key in obj) {
    result.push(obj[key][0]);
    result[result.length-1]["connector"] = key; 
}