I’ve an simple array of string ids and object and on initial load I’m mapping this Ids with this object and making checked property to true
const Ids = ['743156', '743157']
[
{
"id": "743156",
"role": "Authorized Redistributor (AR)",
"checked": true,
"checkBoxPatched": true
},
{
"id": "743157",
"role": "System Of Record (SOR)",
"checked": true,
"checkBoxPatched": true
},
{
"id": "743158",
"role": "Authorized Redistributor (AR)",
"checked": false,
"checkBoxPatched": true
},
{
"id": "743159",
"role": "System Of Record (SOR)",
"checked": false,
"checkBoxPatched": true
},
{
"id": "743976",
"role": "Authorized Redistributor (AR)",
"checked": false,
"checkBoxPatched": true
},
]
Now, user has the option to update by checking checkboxes making checked as true, which might make others Ids checked property to true. How can I get the object whose checked property is true BUT it shouldn’t be initial two objects with checked as true.
So For eg I uncheck checkbox with ID 743156 and check 743158 I should get below two objects and if the user hasn’t checked any NEW checkbox it should return me with empty object {} and not initial checked checkboxes with ids 743156 & 743157 with checked true
{
"id": "743157",
"role": "System Of Record (SOR)",
"checked": true,
"checkBoxPatched": true
},
{
"id": "743158",
"role": "Authorized Redistributor (AR)",
"checked": true,
"checkBoxPatched": true
}
>Solution :
To omit the first two objects having ids of ids array respectively and get the checked values,
const data = [
{
"id": "743156",
"role": "Authorized Redistributor (AR)",
"checked": true,
"checkBoxPatched": true
},
{
"id": "743157",
"role": "System Of Record (SOR)",
"checked": true,
"checkBoxPatched": true
},
{
"id": "743158",
"role": "Authorized Redistributor (AR)",
"checked": false,
"checkBoxPatched": true
},
{
"id": "743159",
"role": "System Of Record (SOR)",
"checked": false,
"checkBoxPatched": true
},
{
"id": "743976",
"role": "Authorized Redistributor (AR)",
"checked": false,
"checkBoxPatched": true
}
];
const ids = ["743156", "743157"];
const checkedArr = [];
data.map((d) => {
if (!ids.includes(d.id) && d.checked === true) {
checkedArr.push(d);
}
})
console.log(checkedArr);