I have a large array of objects with over 10,000 records. Each object contains an array in a specific key value, which needs to be iterated and compared with another array of objects. If there is a match, I want to replace that value with the corresponding object from the comparison array.
Here is an 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"
},
...
]
In obj1, compare the array of technologies with the array of objects in obj2 using IDs. If there is a match, push the matched object into obj1.
The desired output should look 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 attempted to achieve this with the following code snippet:
obj1.map(element => element.technologies.forEach((techArrayList, index) =>
this.operatingSystem.find(o => {
if (techArrayList == o.id) {
obj1[element.technologies].technologies[index].replace(o);
}
})));