Object.assign() and array of objects

三世轮回 提交于 2020-05-17 07:20:06

问题


I have a nested array like this

const names= [[{name: "John"}, {name: "Mary"}],
           [{name: "Paul"}, {name: "Peter"}]];

I would like inject country into the nested object

const country = {country :"USA"}

so that output looks like

[{name: "John", country : "USA"}, {etc} ,{etc} ]

The code idea have is something like this

const combined = names.map((map)=> 
       Object.assign({}, 
              country, 
              /*something to extract name from nested array names*/),
       {country}
         )

Any suggestions how i could spread the object in the nested array to form the desired output?

If the code could be improved in other ways, please let me know as well


回答1:


Make use of reduce to flatten your array along with map and object.assign to add the country value to each object

const names= [[{name: "John"}, {name: "Mary"}],
           [{name: "Paul"}, {name: "Peter"}]];
const country = {country :"USA"};
const newNames = names.reduce((acc, item) =>{
    acc= acc.concat(item.map(value => Object.assign({}, value, country)));
    return acc;
},[]);
console.log(newNames);



回答2:


You can use flat() to create a new array with all sub-array elements concatenated before using map() like the following way:

const names= [[{name: "John"}, {name: "Mary"}],
           [{name: "Paul"}, {name: "Peter"}]];
const country = {country :"USA"}
const combined = names.flat().map(p => Object.assign(p, country));
console.log(combined);



回答3:


It's about using a nested map in an outer map:

const names = [
  [{
    name: "John"
  }, {
    name: "Mary"
  }],
  [{
    name: "Paul"
  }, {
    name: "Peter"
  }]
]

const country = { country: 'USA' }

const output = names.map (xs => xs.map (x => ({ ...x, ...country })))

console.log (output)



回答4:


You might declare a new array literal, list country as the first item, then spread the names[0] array into it, no need for Object.assign nor .map if you only have one sub-array:

const names= [[{name: "John"}, {name: "Mary"}],
           [{name: "Paul"}, {name: "Peter"}]];
const country = {country :"USA"};
const newNames = [[country, ...names[0]]];
console.log(newNames);



回答5:


You can double map the names array and it's nested array and destructure country into each item.

const names = [[{name: "John"}, {name: "Mary"}],
           [{name: "Paul"}, {name: "Peter"}]];
           
const country = {country :"USA"};

const namesWithCountry = names.map(name => name.map(n => ({...n, ...country})));

console.log(namesWithCountry);



回答6:


You can use .flatMap() to flatten the arrays returned by the inner .map() method:

const names= [[{name: "John"}, {name: "Mary"}], [{name: "Paul"}, {name: "Peter"}]],
res = names.flatMap(arr => arr.map(elem => ({...elem, country: 'USA'})));

console.log(res);


来源:https://stackoverflow.com/questions/53389461/object-assign-and-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!