Merging some objects by iterating through an array

Butri

I have the following array

recipe = [
{ 
   id: "abc123", en: {title: "", description: ""}, es: {titleES: "", descriptionES: ""}
},
{ 
   id: "abc128", en: {title: "", description: ""}, es: {titleES: "", descriptionES: ""}
},
{ 
   id: "abc135", en: {title: "", description: ""}, es: {titleES: "", descriptionES: ""}
}
]

and I want to create a new array with 1 object that includes the objects "id" and "en" like this

newArray = [
{ 
   id: "abc123", title: "", description: ""
},
{ 
   id: "abc128", title: "", description: ""
},
{ 
   id: "abc135", title: "", description: ""
}
]

I tried the following but does not seem to work correctly

const dataList = [];
      for (let idx in recipe) {        
        dataList.push({...recipe[idx].id, ...recipeES[idx].all });
        
      }      
mr hr

Traverse the array using array map method and make your object.

const recipe = [
  {
    id: 'abc123',
    en: { title: '', description: '' },
    es: { titleES: '', descriptionES: '' },
  },
  {
    id: 'abc128',
    en: { title: '', description: '' },
    es: { titleES: '', descriptionES: '' },
  },
  {
    id: 'abc135',
    en: { title: '', description: '' },
    es: { titleES: '', descriptionES: '' },
  },
];
const ret = recipe.map((x) => {
  const obj = { id: x.id, ...x.en };
  return obj;
});
console.log(ret);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterating through an array of files

iterating through an array of objects and displaying the items [REACT JS]

Iterating through array - java

Iterating through array

Iterating through an array of Class::Struct objects

Iterating through an array of Objects - Javascript

Iterating through an array of objects using for each or for in

Iterating through object Where Value is Array of Objects and return one value from each Objects of Array of Objects

Iterating through lots of objects

Crash iterating through an array

iterating through array of nested objects with javascript/lodash

Javascript object property 'null' when iterating through an array of objects

Iterating through javascript objects

Iterating through a jQuery array

Iterating through non uniform of array of objects with Angular ng-repeat

Iterating through an array of divs

NullPointerException when iterating through an array of objects

Iterating through JSON objects

Iterating through an objects values

Iterating through array of objects to group all data by date

Iterating through array of objects from your angular 4 firebase database

Create array of objects by iterating through an observable

Iterating through an array of objects to filter specific data

Creating new array by merging array of objects based on some conditions

Iterating through an array of objects and getting the next name property

I am having trouble iterating through an array of objects

Iterating through an array of objects C++

trouble iterating through array containing objects in java

TS error on iterating through an array of objects