将数组中的项插入到一个对象之中

题目描述

将一个数组中的项插入到一个对象中

var dragObj = {
           dragItem: [
            {
              itemName: "正面像",
              itemImgList: []
            },
            {
              itemName: "正面微笑像",
              itemImgList: []
            },
            {
              itemName: "侧面90°像",
              itemImgList: []
            }
          ]
        };
var dataObjArr = [
    {
      "id": 1407,
      "categoryId": "58424971ace94fab9b05dcd0b2134f77",
      "imageName": "正面像",
      "imageUrl": "http://cdn.ormco.synconize.com/ffc9096c809a4f2ba1c8ae2020bce7bc",
      "orderSort": 1
    },
    {
      "id": 1418,
      "categoryId": "58424971ace94fab9b05dcd0b2134f77",
      "imageName": "正面微笑像",
      "imageUrl": "http://cdn.ormco.synconize.com/1cf5931ccb4543769544ff2145291756",
      "orderSort": 2
    },
    {
      "id": 1409,
      "categoryId": "58424971ace94fab9b05dcd0b2134f77",
      "imageName": "侧面90°像",
      "imageUrl": "http://cdn.ormco.synconize.com/38790c6d7d49416f9c181dd726a53abe",
      "orderSort": 3
    }
  ];

目标:生成一个新的对象newObj,

或者通过映射到dragObj
var newObj = {
           dragItem: [
            {
              itemName: "正面像",
              itemImgList: [
                  {
                  "id": 1407,
                  "categoryId": "58424971ace94fab9b05dcd0b2134f77",
                  "imageName": "正面像",
                  "imageUrl": "http://cdn.ormco.synconize.com/ffc9096c809a4f2ba1c8ae2020bce7bc",
                  "orderSort": 1
                    }
              ]
            },
            {
              itemName: "正面微笑像",
              itemImgList: [
                  {
                      "id": 1418,
                      "categoryId": "58424971ace94fab9b05dcd0b2134f77",
                      "imageName": "正面微笑像",
                      "imageUrl":                       "http://cdn.ormco.synconize.com/1cf5931ccb4543769544ff2145291756",
                      "orderSort": 2
                    }
              ]
            },
            {
              itemName: "侧面90°像",
              itemImgList: [
                {
                  "id": 1409,
                  "categoryId": "58424971ace94fab9b05dcd0b2134f77",
                  "imageName": "侧面90°像",
                  "imageUrl": "http://cdn.ormco.synconize.com/38790c6d7d49416f9c181dd726a53abe",
                  "orderSort": 3
                }
              ]
            }
          ]
        };
阅读 2.5k
4 个回答
dragObj.dragItem.forEach(item => {
    item.itemImgList.push(dataObjArr.find(obj => obj.imageName == item.itemName))
})
  1. 如果数据多,不止一条,可以把楼上的 find 改成 filter,选出所有的加进去。
  2. 如果数据比多还多,我觉得可以给 imageName 做个索引,然后遍历 dataObjArr 怼进去,这样的话,遍历的次数更少。
let hash = {};

dataObjArr.forEach(item => {
    hash[item.imageName] = item;
})

dragObj.dragItem.forEach(item => {
    if(hash[item.itemName]) {
        item.itemImgList = [...item.itemImgList, hash[item.itemName]]
    }
})
function arr(){
  dragObj.dragItem.map((i, j) => {
          i.itemImgList.push(dataObjArr[j]);
        return i;
      });
  console.log(dragObj)
};
arr();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题