I have an array of objects that I need to filter. I am wondering if it is possible to modify the object in the filter function. I dont want to use .map then .filter because then it would loop thru the array twice making it take more time and wont scale if there are thousands of items in the list.
const cities = [
{name: 'Los Angeles', population: 100},
{name: 'New York', population: 80},
{name: 'Chicago', population: 120},
{name: 'Houston', population: 60},
{name: 'Philadelphia', population: 70}
];
const highPopulation = cities.filter(item => {
// item.isHighPopulation = true
return item.population >= 100
});
console.log(highPopulation)
Maybe filter isnt the best solution to this problem. I just dont want multiple unessecary loops if I can do this operation in one loop.