I have the following example scenario:
var exampleArrayObjects = [{name: 'Example1'}, {name: 'Example2'}, {name: 'Example3'}];
var exampleArrayValues = ['Example1'];
var result = exampleArrayObjects.map((obj) => {
if(exampleArrayValues.includes(obj.name)){
return {
...obj,
selected: true
}
}else{
return {
...obj,
selected: false
}
}
})
console.log(result);
But in real life scenario, with large amount of data on "exampleArrayObjects" and "exampleArrayValues" it is taking a few minutes to get the result (a result object with additional attribute selected true/false according to the existance of the value).
Is there a faster way to get the same result?
Use a Set
instead for sublinear search times.
const exampleArrayObjects = [{name: 'Example1'}, {name: 'Example2'}, {name: 'Example3'}];
const exampleArrayValues = new Set(['Example1']);
const res = exampleArrayObjects.map(o => ({...o, selected: exampleArrayValues.has(o.name)}));
console.log(res);