Js data processing, how to get the array in the diagram according to the order of fields in type

clipboard.png

type

clipboard.png

Mar.30,2021

var type = ['a', 'b', 'c'];
var list = [
    {name: 'a', age:21},
    {name: 'd', age: 22},
    {name: 'c', age: 33}
]
list.filter((item)=> {
    return type.includes(item.name);
})

if it's helpful, please take it. Thank you

=

var type = ['a', 'b', 'c'];
var list = [
    {name: 'd', age: 22},
    {name: 'c', age: 33},
    {name: 'a', age: 11}    
]
const result = [];
type.forEach((t) =>{
    list.forEach((item) =>{
        if(t === item.name) {
            result.push(item);
        }
    })
})

  

create an empty array, loop through type, and then traverse list, to compare type [I] with list [nane], and push to the new array

Menu