I have an array of objects like:
[{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}]
The maximum number of objects in the array is around 7600
I am struggling to find a fast way of flipping the 'selected' property from true to false or vice versa of every object whose id is included in an array of id values like:
['123abc322', '123abc323']
The supplier array of id values could include ALL the object ids and will often be ~2000 ids.
So if the object.id field is in the supplied array I want to swap the object.selected to !object.selected
Any help would be appreciated.
I would recommend converting your array of IDs into a Set. Then, it's a matter of doing one pass over your array and, if the Set contains the element's ID, toggle the selected property.
Note that this method modifies the objects in the array. If you need to not mutate the input, you could use a map.
Mutating the input:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
arr.forEach(el => {
if (ids.has(el.id)) {
el.selected = !el.selected;
}
});
console.log(arr);
Not mutating the input:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
const newArr = arr.map(el => {
if (!ids.has(el.id)) return el;
return {
...el,
selected: !el.selected
}
});
console.log(newArr);
You can do everything with a single line of code, here's a code example:
let array = [{
type: "marker",
name: "somename",
id: "123abc321",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc322",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc323",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
];
let ids = ['123abc322', '123abc323'];
// .filter() create a new array with all elements that pass the test implemented by the provided function.
// .map() run a function for everyone of the remaining objects and flip the "selected" value.
array.filter(x => ids.includes(x.id)).map(x => x.selected = !x.selected);
console.log(array);
Hope It's what your looking for.