skip to Main Content

I do have big array of objects as response which contains more than 10k records, in each object in specific key value contains array, that should iterate and compare with another array of objects and if it matches, i want to replace that value with object by comparing value with array of object.

here is the example.

obj1 = [{
  "control_id": "red1234",
  "security_domain": "astrem",
  "control_statement": "testing",
  "descriptio": "test",
  "label": "minimal",
  "technologies": [
    "180"
  ],
  "reference": {
    "string": "string"
  },
  "evaluation": "",
  "category": null
}, {
  "control_id": "red1234",
  "security_domain": "astrem",
  "control_statement": "testing",
  "descriptio": "test",
  "label": "minimal",
  "technologies": [
    "180", "320","3213"
  ],
  "reference": {
    "string": "string"
  },
  "evaluation": "",
  "category": null
}]
obj2 = [
  {
    "id": 94,
    "name": "SUSE Linux Enterprise 12.x"
  },
  {
    "id": 174,
    "name": "Ubuntu 18.x"
  },
  {
    "id": 106,
    "name": "Windows 2016 Server"
  },
  {
    "id": 180,
    "name": "Windows 2019 Server"
  },
  {
    "id": 53,
    "name": "Windows 2012 Server"
  },
  {
    "id": 217,
    "name": "Red Hat Enterprise Linux 8.x"
  },
  {
    "id": 81,
    "name": "Red Hat Enterprise Linux 7.x"
  },
  {
    "id": 109,
    "name": "Amazon Linux AMI"
  },
  {
    "id": 126,
    "name": "Amazon Linux 2 AMI"
  },
  {
    "id": 0,
    "name": "All OSs"
  },
  {
    "id": 1,
    "name": "Windows Platforms"
  },
  {
    "id": 2,
    "name": "Linux Platforms"
  },
  {
     "id": 320,
     "name": "Windows 2012 Server"
   },
   {
     "id": 3213,
     "name": "Windows 1999 Server"
   }
]

big data is obj 1, in obj1 compare array of technologies with obj2 array of object list with id and if it matches push value with matched object into obj1.

output should be like this

obj1 = [
  {
    "control_id": "red1234",
    "security_domain": "astrem",
    "control_statement": "testing",
    "descriptio": "test",
    "label": "minimal",
    "technologies": [
      {
        "id": 180,
        "name": "Windows 2019 Server"
      }
    ],
    "reference": {
      "string": "string"
    },
    "evaluation": "",
    "category": null
  },
  {
    "control_id": "red1234",
    "security_domain": "astrem",
    "control_statement": "testing",
    "descriptio": "test",
    "label": "minimal",
    "technologies": [
      {
        "id": 180,
        "name": "Windows 2019 Server"
      },
      {
        "id": 320,
        "name": "Windows 2012 Server"
      },
      {
        "id": 3213,
        "name": "Windows 1999 Server"
      }
    ],
    "reference": {
      "string": "string"
    },
    "evaluation": "",
    "category": null
  }
]

I tried this

obj1.map(element => element.technologies.forEach((techArrayList, index) => 
     this.operatingSystem.find(o => {
      if (techArrayList == o.id) {
         obj1[element.technologies].technologies[index].replace(o);
      }
     })));

2

Answers


  1. Here’s what I would do…

    /**
     * First, to make things much simpler and more time efficient, 
     * create an object with entries like this that can be used for direct reference:
     * {
     *   1: { id: 1, name: 'Windows Platforms },
     *   2: { id: 2, name: 'Linux Platforms' }
     *   ...
     * }
     */
    const technologyMap = obj2.reduce((accumulator, technology) => {
        // for every item in the array, create a new property 
        // in the technologyMap that has a key of the 'id' property
        // and a value of the original object
        // e.g., { '1': { id: 1, name: 'Windows Platforms } }
        // then you can reference technologyMap['1'] and get { id: 1, name: 'Windows Platforms }
        // you can also use forEach to do this, but I prefer the side effect free functions
        accumulator[technology['id']] = { ...technology };
        return accumulator;
    }, {});
    
    /**
     * Next, create a new list, replacing all technology ids with the objects they reference
     */
    const newBigDataList = obj1.map((dataItem) => {
        // create an array of technology objects from the techIds
        const technologies = dataItem.technologies.map((techId) => ({ ...technologyMap[techId] }));
        
        // return a new object with all dataItem properties, 
        // replacing the technologies array with the new array of objects
        return { ...dataItem, technologies };
    });
    
    /**
     * If your big data list is too big and you don't want to create a new list,
     * you can just update the technologies arrays like this, but I generally prefer immutable data if possible
     */
    obj1.forEach((dataItem) => {
        // create an array of technology objects from the techIds
        const technologies = dataItem.technologies.map((techId) => ({ ...technologyMap[techId] }));
    
        // update the existing dataItem with the new array of technology objects
        dataItem.technologies = technologies;
    });
    
    Login or Signup to reply.
  2. Initially map the value from obj1 & find in obj2

    obj1.forEach((o) => {
          let arr = o.key.map((k) => {
            const nameObject = obj2.find((ele) => {
              return ele.id == k;
            });
            return nameObject;
          });
          o.key = arr;
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search