2

I'm struggling of coping with an issue here. lets suppose I have an array of objects like this:

[
 {name:Alex, products: [{type: 1, code: 1213}, {type: 2, code: 2312}]},
 {name:Alex, products: [{type: 1, code: 1213}, {type: 2, code: 2312}]}
]

I would like somehow to reconstruct my object to be something like this:

[
 {name:Alex, products: [{name:Alex, type: 1, code: 1213}, {name:Alex, type: 2, code: 2312}]},
 {name:Alex, products: [{{name:Alex, type: 1, code: 1213}, {{name:Alex, type: 2, code: 2312}]}
]

so each nested array contains the name. Is that possible in javascript?

3
  • 3
    Is it possible in javascript? Yes. Commented Jan 31, 2018 at 14:16
  • Could you so me the way to do this? Commented Jan 31, 2018 at 14:17
  • 4
    See below for spoon feeding, where you get to brush up on your copy/paste skills. Alternatively, look here and learn how to do it :) Commented Jan 31, 2018 at 14:20

5 Answers 5

1

You can use map() method with spread syntax in object to create new array with updated objects and keep original data.

const data = [
 {name:'Alex', products: [{type: 1, code: 1213}, {type: 2, code: 2312}]},
 {name:'Alex', products: [{type: 1, code: 1213}, {type: 2, code: 2312}]}
]

const result = data.map(e => ({
  ...e,
  products: e.products.map(p => {
    return {...p, name: e.name}
  })
}))

console.log(result)

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

Comments

1

I want somehow to reconstruct my object so as to be something like this:

Use map

arr.map( s => (s.products.map( i => (i.name = s.name, i) ), s) );

Demo

var arr = [{
    name: "Alex",
    products: [{
      type: 1,
      code: 1213
    }, {
      type: 2,
      code: 2312
    }]
  },
  {
    name: "Alex",
    products: [{
      type: 1,
      code: 1213
    }, {
      type: 2,
      code: 2312
    }]
  }
]
var output = arr.map(s => (s.products.map(i => (i.name = s.name, i)), s));

console.log(output);

1 Comment

Interestingly this is the only correct answer, as rightly you have noticed the word reconstruct, all other answer are assuming the OP wanted copy.
1

You could create new array/objects with Object.assign.

var data = [{ name: 'Alex', products: [{ type: 1, code: 1213 }, { type: 2, code: 2312 }] }, { name: 'Alex', products: [{ type: 1, code: 1213 }, { type: 2, code: 2312 }] }],
    converted = data.map(o => Object.assign(
        {},
        o,
        { products: o.products.map(p => Object.assign({ name: o.name }, p)) }
    ));
    
console.log(converted);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

You can use nested Array.map() calls with Object.assign() to create the new array without changing the original data:

const data = [{"name":"Alex","products":[{"type":1,"code":1213},{"type":2,"code":2312}]},{"name":"Alex","products":[{"type":1,"code":1213},{"type":2,"code":2312}]}]

const result = data.map((o) => Object.assign({}, o, {
  products: o.products.map((p) => Object.assign({ name: o.name }, p))
}))

console.log(result)

If you just want to change (mutate) the current array, use nested Array.forEach() calls:

const data = [{"name":"Alex","products":[{"type":1,"code":1213},{"type":2,"code":2312}]},{"name":"Alex","products":[{"type":1,"code":1213},{"type":2,"code":2312}]}]

data.forEach((o) => o.products.forEach((p) => p.name = o.name));

console.log(data)

Comments

1

You have typo in name property.Alex should be 'Alex' (string).You can get help from two array methods. Array.prototype.map() and Array.prototype.forEach() to achieve your goal

var initialArr = [
 {name:'Alex1', products: [{type: 1, code: 1213}, {type: 2, code: 2312}]},
 {name:'Alex2', products: [{type: 1, code: 1213}, {type: 2, code: 2312}]}
]

var newArr = initialArr.map(function(elem){
    elem.products.forEach(function(obj){
        obj.name = this.name
    },elem)
    return elem
})
console.log(newArr)

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.