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 });
}
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.
Comments