Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript check if array values exist in array of objects

Hi I’ve an array and I’ve an array of objects

const factArr = ['743156', '743157']

[
    {
        "id": null,
        "name": null,
        "adsFactId": "743156"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743158",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743159",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743976",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "744961",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "746809"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "747229"
    },
    {
        "adsId": null,
        "name": null,
        "adsFactId": "747231"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "749059"
    }
]

I want to check factArr values exists within this object, equal to(adsFactId). If yes I want to add two properties for that object checked:true, checkBoxPatched: true

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so the O/P should be

[
    {
        "id": null,
        "name": null,
        "adsFactId": "743156",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
        "checked": true,
        "checkBoxPatched": true
    }
    //with the other objects
]

Anyways we can achieve it

>Solution :

const factArr = ['743156', '743157'];
const arr = [
    {
        "id": null,
        "name": null,
        "adsFactId": "743156"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743158",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743159",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743976",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "744961",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "746809"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "747229"
    },
    {
        "adsId": null,
        "name": null,
        "adsFactId": "747231"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "749059"
    }
];

arr.some(obj => {
  if(factArr.includes(obj['adsFactId'])) {
    obj.checked = true;
    obj.checkBoxPatched = true;
  } else {
    obj.checked = false;
    obj.checkBoxPatched = false;
  }
});
console.log(arr);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading