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
Here’s what I would do…
Initially map the value from obj1 & find in obj2