Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to push all items from array of object to an array?

I am trying to extract and push the elements from an array of object to an empty object.
Here is the example of the arrays.

const products = ["cars", "phones", "laptops"];

const productArrayOfObj = [
  {
    cars: ["Ford", "Chevy", "Honda", "Toyota"],
  },
  {
    phones: ["iPhone", "Samsung", "Nokia", "Sony"],
  },
  {
    laptops: ["Mac", "Dell", "HP", "Acer"],
  },
];

const mappedProducts= products.map((_,i) => productArrayOfObj[i]);

From mappedProducts returns this structure, even though I am not sure if this is the correct way of mapping this.

 [ { cars: [ 'Ford', 'Chevy', 'Honda', 'Toyota' ] }, 
  { phones: [ 'iPhone', 'Samsung', 'Nokia', 'Sony' ] }, 
  { laptops: [ 'Mac', 'Dell', 'HP', 'Acer' ] } ]

My desired array would look like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ 'Ford', 'Chevy', 'Honda', 'Toyota','iPhone', 'Samsung', 'Nokia',
  'Sony','Mac', 'Dell', 'HP', 'Acer' ]

The way I’m trying to implement it, returns wrong result.

const arrayToPush = []
const getArrayEl = mappedProducts && mappedProducts.map(item => { 
for(let key in item){
    arrayToPush.push(item[key])
}
  return arrayToPush
})

Any help will be appreciated

const names = ["cars", "phones", "laptops"];

const nameArrayOfObj = [
  {
    cars: ["Ford", "Chevy", "Honda", "Toyota"],
  },
  {
    phones: ["iPhone", "Samsung", "Nokia", "Sony"],
  },
  {
    laptops: ["Mac", "Dell", "HP", "Acer"],
  },
];

const mappedProducts = names && nameArrayOfObj && names.map((_,i) => nameArrayOfObj[i]);

console.log("mappedProducts", mappedProducts);
const arrayToPush = []
const getArrayEl = mappedProducts && mappedProducts.map(item => { 
for(let key in item){
    arrayToPush.push(item[key])
}
  return arrayToPush
})
console.log("getArrayEl", getArrayEl);

>Solution :

Array.flatMap implementation.

Logic

  • Loop through names array with Array.flatMap.
  • Access each keys from nameArrayOfObj.

This implementation makes use of an asumprion that both arrays are in same order.

Working Fiddle

const names = ["cars", "phones", "laptops"];
const nameArrayOfObj = [
  { cars: ["Ford", "Chevy", "Honda", "Toyota"] },
  { phones: ["iPhone", "Samsung", "Nokia", "Sony"] },
  { laptops: ["Mac", "Dell", "HP", "Acer"] },
];
const mappedProducts = names.flatMap((name, i) => nameArrayOfObj[i][name] ? nameArrayOfObj[i][name] : []);
console.log(mappedProducts);

If you cannot ensure the arrays ae in same order, you can make use of this logic.

  • Loop through names Array.
  • Find an item from nameArrayOfObj array with the key in names Array.

Working Fiddle

const names = ["cars", "phones", "laptops"];
const nameArrayOfObj = [
  { cars: ["Ford", "Chevy", "Honda", "Toyota"] },
  { phones: ["iPhone", "Samsung", "Nokia", "Sony"] },
  { laptops: ["Mac", "Dell", "HP", "Acer"] },
];
const mappedProducts = names.map((name, i) => {
  const matchingNode = nameArrayOfObj.find(item => item.hasOwnProperty(name));
  return matchingNode ? matchingNode[name] : [];
});
console.log(mappedProducts);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading