1

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

2
  • Should country be add to all array elements or just the first element in the nested array? Commented Nov 20, 2018 at 9:06
  • So you want to add another key to every object? But is the expected output a similarly nested array of objects or just a flat array of objects? And should the old objects and arrays be reused? And will the array be always nested exactly one level deep? Commented Apr 23, 2020 at 4:19

6 Answers 6

5

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

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

Comments

2

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

Comments

1

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)

Comments

0

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

Comments

0

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

Comments

0

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

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.