阿波羅的戰(zhàn)車
2023-07-06 14:57:46
我有以下數(shù)組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: ""}}]我想創(chuàng)建一個包含 1 個對象的新數(shù)組,其中包含對象“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 }); }
1 回答

慕田峪9158850
TA貢獻1794條經(jīng)驗 獲得超8個贊
使用數(shù)組映射方法遍歷數(shù)組并創(chuàng)建對象。
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);
添加回答
舉報
0/150
提交
取消