undefinedfix
Sign in

Inserts items in an array into an object

Perfectdiets edited in Thu, 14 Jul 2022

Title Description

Inserts items from an array into an object

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
    }
  ];

Objective: to generate a new object, newobj ,

Or by mapping to 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
                }
              ]
            }
          ]
        };
4 Replies
Red
commented on Thu, 14 Jul 2022
dragObj.dragItem.forEach(item => {
    item.itemImgList.push(dataObjArr.find(obj => obj.imageName == item.itemName))
})
suxbr
commented on Thu, 14 Jul 2022
  1. If there are more than one data, you can change the find upstairs to filter and select all the data to add in.
  2. If there is more data than more, I think we can index imagename, traverse dataobjrr and connect it. In this way, the number of traversals will be less.
sqcrr
commented on Thu, 14 Jul 2022
let hash = {};

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

dragObj.dragItem.forEach(item => {
    if(hash[item.itemName]) {
        item.itemImgList = [...item.itemImgList, hash[item.itemName]]
    }
})
edwinnosh
commented on Thu, 14 Jul 2022
function arr(){
  dragObj.dragItem.map((i, j) => {
          i.itemImgList.push(dataObjArr[j]);
        return i;
      });
  console.log(dragObj)
};
arr();