为了账号安全,请及时绑定邮箱和手机立即绑定

通过迭代数组来合并一些对象

通过迭代数组来合并一些对象

阿波罗的战车 2023-07-06 14:57:46
我有以下数组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 });              }     
查看完整描述

1 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

使用数组映射方法遍历数组并创建对象。


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);


查看完整回答
反对 回复 2023-07-06
  • 1 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号