I know that there is a similar question already answered, but mine is different in a way that I just don't want to find it, I just want to update it
I have a JS array of objects like this:
let data = [
{
contact_id: 15,
use_id: 16,
name: 'ABC',
phoneNo: '0092123456789',
checked: false,
},
{
contact_id: 16,
use_id: 22,
name: 'DEF',
phoneNo: '0092123456788',
checked: false,
},
{
contact_id: 17,
use_id: 24,
name: 'XYZ',
phoneNo: '0092123456787',
checked: false,
}
];
Now with this in mind, I have a property named phoneNo
which guaranteed unique values.
Now what I want is to toggle the value of checked
for a given number.
What I have tried:
for (let i = 0; i < data.length; i++) {
if (data[i]['phoneNo'] === item.phoneNo) {
data[i]['checked'] = !data[i]['checked'];
break; // to make it a little efficent :-)
}
}
I wrote this code and it worked fine for a while but now I have realized that the list is growing larger and larger and this code iterates over and over again, which is very inefficient.
What I want?
Is there a way that I could directly toggle the value of the property checked
given the value of phoneNo
without looping over every single item?