简体   繁体   English

将Javascript对象和子对象转换为对象数组-Lodash

[英]Convert Javascript object and children into array of objects - lodash

I am looking to change the structure of some data. 我正在寻找改变一些数据的结构。 Below is a sample of the data I am receiving 以下是我收到的数据样本

{
  ID1: {
         firstName: 'John',
         lastName: 'Doe'
      },
  ID2: {
         firstName: 'Jane',
         lastName: 'Doe'
      }
}

What I need to do is convert this into an array with the following format: 我需要做的是将其转换为以下格式的数组:

[
  { ID: ID1, firstName: 'John', lastName: 'Doe' },
  { ID: ID2, firstName: 'Jane', lastName: 'Doe' }
]

I prefer to use lodash if possible 如果可能,我更喜欢使用lodash

Use Object.keys() and Array#map() . 使用Object.keys()Array#map() Object.keys() which helps to get all the properties of the object as an array. Object.keys()有助于以数组的形式获取对象的所有属性。 Then Array#map() can be used to iterate over the element and generate new array based on the returned element. 然后,可以使用Array#map()迭代元素,并根据返回的元素生成新的数组。

 var data = { ID1: { firstName: 'John', lastName: 'Doe' }, ID2: { firstName: 'Jane', lastName: 'Doe' } }; var res = Object.keys(data) // get all object properties as an array .map(function(k) { // iterate and generate new array with custom element return { // generate custom array object based on the property and object and return ID: k, // k is property of ov=bjeck firstName: data[k].firstName, // get inner properties from data object using k larstName: data[k].lastName }; }) console.log(res); 


UPDATE : I've created new object instead of updating original object and returned it. 更新:我创建了新对象,而不是更新原始对象并返回了它。 If updating original object is not a problem then add additional property to object and return the reference inside the Array#map() callback function. 如果更新原始对象不是问题,则向对象添加其他属性,并在Array#map()回调函数内返回引用。 This works if there is any number of inner properties but it updates original object. 如果存在许多内部属性,则此方法有效,但是会更新原始对象。

 var data = { ID1: { firstName: 'John', lastName: 'Doe' }, ID2: { firstName: 'Jane', lastName: 'Doe' } }; var res = Object.keys(data) // get all object properties as an array .map(function(k) { // iterate and generate new array with custom element data[k].ID = k; // add additional property return data[k]; // return the reference }) console.log(res); 


Or in case you want to maintain the object and there is lots of inner object properties then do something like. 或者,如果您要维护对象并且内部对象属性很多,则可以执行类似操作。

 var data = { ID1: { firstName: 'John', lastName: 'Doe' }, ID2: { firstName: 'Jane', lastName: 'Doe' } }; var res = Object.keys(data) // get all object properties as an array .map(function(k) { // iterate and generate new array with custom element var obj = { // create an object to return ID: k }; for (var key in data[k]) // iterate over object property if (data[k].hasOwnProperty(key)) // check the object has the property obj[key] = data[k][key]; // add property to the newly generated object return obj; // return the object }) console.log(res); 

Can you add ID to the objects? 您可以向对象添加ID吗? If so, then it can be achieved easy with: 如果是这样,则可以使用以下方法轻松实现:

var arr = _.values(obj);

Explanation: 说明:

Often developers duplicate key inside value to avoid many problems. 开发人员通常会复制密钥内部值以避免很多问题。 They do like this: 他们确实是这样的:

{
  ID1: {
         ID: ID1,
         firstName: 'John',
         lastName: 'Doe'
      },
  ID2: {
         ID: ID2
         firstName: 'Jane',
         lastName: 'Doe'
      }
}

And then use only one command above and it returns only values of the object and make an array. 然后,在上方仅使用一个命令,它仅返回对象的值并创建一个数组。

If it is not suites for you, then you need to add IDs manually like that: 如果不是适合您的套件,则需要像这样手动添加ID:

var arr = _.map(obj, function(value, key) {
  value[ID] = key;
  return value;
});

Be careful, the code will change the original object, but in most of cases it is not a problem, but if you need to avoid this, you need: 请注意,代码将更改原始对象,但在大多数情况下这不是问题,但是如果需要避免这种情况,则需要:

var arr = _(obj)
  .clone()
  .map(obj, function(value, key) {
    value[ID] = key;
    return value;
  })
  .value();

You can make use of map() to convert the object into an array and then use assign() to append the ID key value, this also makes sure that the original object does not get mutated. 您可以使用map()将对象转换为数组,然后使用Assign()附加ID键值,这还可以确保原始对象不会发生突变。

Here is the ES6 version: 这是ES6版本:

var result = _.map(data, (item, ID) => _.assign({ ID }, item));

 var data = { ID1: { firstName: 'John', lastName: 'Doe' }, ID2: { firstName: 'Jane', lastName: 'Doe' } }; var result = _.map(data, (item, ID) => _.assign({ ID }, item)); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script> 

Here's the es5 version: 这是es5版本:

var result = _.map(data, function(item, ID) { 
  return _.assign({ ID: ID }, item);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM