2

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,

2
  • Will wishlist objects (e.g. {product1 : 1} ) ever have more than one entry? Commented May 12, 2021 at 9:35
  • I want something reverse of this. could anybody help me? Commented Sep 13, 2021 at 19:38

5 Answers 5

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));

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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);

Comments

0

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);

3 Comments

You can't delete wishlist property of original obj.
@RahulKumar What do you mean by can't? This code appears to work. The question doesn't mention anything about not modifying the original object. Do you just mean that it would be better practice not to? Or is there something I'm missing?
It's recommended to not modify original object while following best coding practices. In his code that object might be used somewhere else and it can cause issue.
0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.