Js uses for to loop an array of Filter objects

    let objList = [
        {name:"tom",age:12},
        {name:"jack",age:33},
        {name:"zio",age:12},
        {name:"lolo",age:89},
        {name:"robin",age:16},
        ]
    
    let ageList = [12,16];
    let getData = objList.filter((item)=>{
        return ageList.includes(item.age)
    });
    
    console.log(getData)

as shown above, the final printed data is an element with an age of 12 / 16. I would like to ask you bosses, how can the Filter object array get the desired data if you only use the for loop method?

Feb.20,2022

this title has nothing to do with the algorithm. Okay, it's just a basic judgment.

let getData = [];
for (let i = 0; i < objList.length; iPP) {
  let item = objList[i];
  if (ageList.includes(item.age)) {
    getData.push(item);
  }
}
console.log(getData)

and filter can be simplified to

 let getData = objList.filter(({age}) => ageList.includes(age));
The result of

2 pieces of code is the same. There is no need to write the for loop. Anyway, the online code should be compiled to es5

.

if you don't consider order , you can first convert data source from Array to Map.Array , and then use Filter condition to extract the desired data one by one:

var newArr=[];
for(var i=0;i<objList.length;iPP)
  ageList.indexOf(objList[i].age)!=-1&&(newArr.push(objList[i]));
Menu