According to the key value, js selects the items that meet the conditions to form a new array.

topic description

there is an array, such as let arr = ["banana", "apple", "pear"]; , and then another array of objects:

let arr = ["",""""];


// 
let allArr = [
    {id:"001",name:"",price:"2"},
    {id:"002",name:"",price:"1"},
    {id:"003",name:"",price:"20"},
    {id:"004",name:"",price:"22"},
    {id:"005",name:"",price:"26"}
];


// :arr;
// 
let finalArr = [
    {id:"002",name:"",price:"1"},
    {id:"001",name:"",price:"2"},
    {id:"003",name:"",price:"20"}
];
Jan.10,2022

   arr.forEach((a) => {
       allArr.forEach((all) => {
          if(a === all.name){
             finalArr.push(all);
          }
       })
    })

  

allArr.filter (I = > arr.some (_ I = > i.name = _ i)). Sort ((a, b) = > arr.indexOf (a.name)-arr.indexOf (b.name) > = 0? 1:-1)

Menu