简体   繁体   English

在 object 中创建一个对象数组作为多个对象 JavaScript

[英]Make an array of objects inside object as multiple objects JavaScript

I need to transform an array of objects in multiple objects inside parent object:我需要在父 object 内的多个对象中转换对象数组:

Actual object:实际 object:

{
name: 'John Doe',
Age: 50,
email: 'j@gmail.com'
wishlist: [
   {product1 : 1},
   {product2 : 3},
   {product3 : 5},
   {product4 : 2},
 ]
}

Goal:目标:

{

name: 'John Doe',
Age: 50,
email: 'j@gmail.com',
product1 : 1,
product2 : 3,
product3 : 5,
product4 : 2,

}

Does anyone know how to do that?有谁知道这是怎么做到的吗? kind regards,亲切的问候,

Use reduce() method on wishlist array and then create final object using spread operator.在愿望清单数组上使用reduce()方法,然后使用spread运算符创建最终的 object。

 const myObj = { name: 'John Doe', Age: 50, email: 'j@gmail.com', wishlist: [ {product1: 1}, {product2: 3}, {product3: 5}, {product4: 2}, ] } const processData = (data) => { const wishlist = data.wishlist.reduce((result, obj) => { return {...result, ...obj}; }, {}); const finalObj = {...data, ...wishlist}; delete finalObj.wishlist; return finalObj; } console.log(processData(myObj));

You can merge all products, then merge them with the object and then delete the initial array.您可以合并所有产品,然后将它们与 object 合并,然后删除初始数组。 I think this way is better, not to modify the original object while iterating one of its attributes.我认为这种方式更好,不要在迭代它的一个属性时修改原来的 object。

let products = {} 
for (let product of obj.wishlist)
   products = {...products, ...product}
obj = {...obj, ...products}
delete obj.wishlist

You could use destructuring to grab your wishlist array, and an object of properties excluding your whishlist array (stored in r ), which you can then use Object.assign() with the spread syntax to merge all the objects from your wishlist array into your r object: You could use destructuring to grab your wishlist array, and an object of properties excluding your whishlist array (stored in r ), which you can then use Object.assign() with the spread syntax to merge all the objects from your wishlist array into your r object:

 const {wishlist, ...r} = { name: 'John Doe', Age: 50, email: 'j@gmail.com', wishlist: [ {product1: 1}, {product2: 3}, {product3: 5}, {product4: 2}, ] }; const res = Object.assign(r, ...wishlist); console.log(res);

You can try this,你可以试试这个

var obj = {
  name: 'John Doe',
  Age: 50,
  email: 'j@gmail.com',
  wishlist: [{ product1: 1 }, { product2: 3 }, { product3: 5 }, { product4: 2 }]
};

var newObj = Object.assign({}, ...obj.wishlist);
delete obj.wishlist;

const finalObj = { ...obj, ...newObj };
console.log(finalObj);

If anyone was looking for a more generic answer.如果有人在寻找更通用的答案。

I have written code to transform an Object or an Array inside another Object or an Array.我编写了代码来转换 Object 或另一个 Object 或数组中的数组。

Overkill if you ask me.如果你问我,那就过分了。

 const sample = [{ name: 'John Doe', Age: 50, email: 'j@gmail.com', wishlist: [{ product1: 1 }, { product2: 3 }, { product3: 5 }, { product4: 2 }] }]; const transformArray = array => { let obj = {}; array.forEach(item => { if (Array.isArray(item)) { obj = {...obj, ...transformArray(item)}; } else if (typeof item == 'object') { obj = {...obj, ...transformObj(item)}; } }); return obj; } const transformObj = object => { let obj = {}; Object.keys(object).forEach(key => { const item = object[key]; if (Array.isArray(item)) { obj = {...obj, ...transformArray(item) }; } else if (typeof item == 'object') { obj = {...obj, ...transformObj(item) }; } else { obj = {...obj, [key]: item }; } }); return obj; } console.log(transformObj(sample));

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

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