简体   繁体   中英

Reconstruct an array of objects in javascript that contains nested arrays

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?

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

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

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) 

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) 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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