How to filter this array

let list = [
        {name: "tom", age: 10},
        {name: "lee", age: 11},
        {name: "rose", age: 12},
        {name: "jane", age: 13}
    ]
let info = [
    {name: "lee"},
    {name: "rose"}
]

filter out what is not in info from list,
except for those that make up a new array, are there any direct filtering methods that do not meet the criteria, ways to change the original array or others, to gather wisdom, gods


let newList=[]
for(let i=0;i<list.length;iPP){
    for(let j=0;j<info.length;jPP){
        if (list[i].name===info[j].name){
            newList.push(list[i])
        }
    }
}
Jun.07,2022

let list = [
  { name: 'tom', age: 10 },
  { name: 'lee', age: 11 },
  { name: 'rose', age: 12 },
  { name: 'jane', age: 13 }
]
let info = [
  { name: 'lee' },
  { name: 'rose' }
]

let new_info = JSON.stringify(info);
const new_list = list.filter(function (val, ind) {
  // list
  if (new_info.indexOf(val.name)<0) {
    return true;
  }
})

console.log(new_list);
< hr >

modify let new_info = JSON.stringify (info); to
JSON.stringify (info). Split ('name'). Join (); more robust;


let names = info.map(val => val.name);
let newList = list.filter(val => names.indexOf(val.name) > -1);

if the data type of info can be changed, just change it to a Map, and then the key value points to a property that can represent an object, for example, you may be name here.

The filtering logic after

is to traverse the list, and then filter it based on whether the unique identification attribute exists in the Map, complexity O (n)


const newList = list.filter(val => info.some(item => item.name === val.name))

let list = [
    { name: 'tom', age: 10 },
    { name: 'lee', age: 11 },
    { name: 'rose', age: 12 },
    { name: 'jane', age: 13 }
]
let info = [
    { name: 'lee' },
    { name: 'rose' }
]
let arr = [];
let crr = [];

function Fn(list, callback, arr) {

    list.filter(function(i, index) {
        if (callback(i)) {
            arr.push(i);
            //return true;
        } else {
            return false;
        }
    })

    return arr;
}

let Brr = Fn(list, function(data) {
    let Me = false
    for (let i = 0; i < info.length; iPP) {
        if (info[i].name === data.name) {
            Me = true;
            break;
        } else {
            Me = false;
        }
    }
    if (Me) {
        console.log(1);
        return true;
    } else {
        console.log(2);
        return false;
    }
}, arr)


console.log(Brr);


const filterList = (list, info) => {
  return list.filter(item => {
    return [...new Set(info.map(i=>i.name))].includes(item.name)
  })
}

let info1 = [];
info.map(e=>info1.push(e.name))
let arr = list.filter(e=>info1.includes(e.name))     
console.log(arr) 

clipboard.png


    // info  key 
    let info = [
        {name:'lee'},                     
        {name:'rose'}
    ].map(function(item){
        return item.name;
    });
    
    // splice
    list.forEach(function(item, index, arr){
        if(!info.includes(item.name)){
            arr.splice(index,1)
        }
    });
    
    //   filter
    let result = list.filter(function(item, index, arr){
        return info.includes(item.name)
    });
Menu