通过遍历数组合并一些对象
Merging some objects by iterating through an 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: ""}
}
]
我想创建一个包含 1 个对象的新数组,其中包含对象“id”和“en”,如下所示
newArray = [
{
id: "abc123", title: "", description: ""
},
{
id: "abc128", title: "", description: ""
},
{
id: "abc135", title: "", description: ""
}
]
我尝试了以下方法,但似乎无法正常工作
const dataList = [];
for (let idx in recipe) {
dataList.push({...recipe[idx].id, ...recipeES[idx].all });
}
使用数组映射方法遍历数组并创建对象。
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);
我有以下数组
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: ""}
}
]
我想创建一个包含 1 个对象的新数组,其中包含对象“id”和“en”,如下所示
newArray = [
{
id: "abc123", title: "", description: ""
},
{
id: "abc128", title: "", description: ""
},
{
id: "abc135", title: "", description: ""
}
]
我尝试了以下方法,但似乎无法正常工作
const dataList = [];
for (let idx in recipe) {
dataList.push({...recipe[idx].id, ...recipeES[idx].all });
}
使用数组映射方法遍历数组并创建对象。
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);