最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Make an array of objects inside object as multiple objects JavaScript - Stack Overflow

programmeradmin1浏览0评论

I need to transform an array of objects in multiple objects inside parent object:

Actual object:

{
name: 'John Doe',
Age: 50,
email: '[email protected]'
wishlist: [
   {product1 : 1},
   {product2 : 3},
   {product3 : 5},
   {product4 : 2},
 ]
}

Goal:

{

name: 'John Doe',
Age: 50,
email: '[email protected]',
product1 : 1,
product2 : 3,
product3 : 5,
product4 : 2,

}

Does anyone know how to do that? kind regards,

I need to transform an array of objects in multiple objects inside parent object:

Actual object:

{
name: 'John Doe',
Age: 50,
email: '[email protected]'
wishlist: [
   {product1 : 1},
   {product2 : 3},
   {product3 : 5},
   {product4 : 2},
 ]
}

Goal:

{

name: 'John Doe',
Age: 50,
email: '[email protected]',
product1 : 1,
product2 : 3,
product3 : 5,
product4 : 2,

}

Does anyone know how to do that? kind regards,

Share Improve this question asked May 12, 2021 at 9:21 futuraVitafuturaVita 231 silver badge5 bronze badges 2
  • Will wishlist objects (e.g. {product1 : 1} ) ever have more than one entry? – Ben Stephens Commented May 12, 2021 at 9:35
  • I want something reverse of this. could anybody help me? – AbhiSam Commented Sep 13, 2021 at 19:38
Add a ment  | 

5 Answers 5

Reset to default 2

Use reduce() method on wishlist array and then create final object using spread operator.

const myObj = {
name: 'John Doe',
Age: 50,
email: '[email protected]',
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. I think this way is better, not to modify the original object while iterating one of its attributes.

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:

const {wishlist, ...r} = { name: 'John Doe', Age: 50, email: '[email protected]', 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: '[email protected]',
  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.

Overkill if you ask me.

const sample = [{
  name: 'John Doe',
  Age: 50,
  email: '[email protected]',
  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));

发布评论

评论列表(0)

  1. 暂无评论