var a = [100,200,300]
var b = [{id:'100',name:'小红'},{id:'300',name:'小明'},{id:'200',name:'小蓝'}]
function sortSome(a, b){
var arr = [];
a.forEach(function(tem, index){
b.forEach(function(val, num){
if(tem == val.id){
arr.push(val);
}
})
})
return arr;
}
console.log(sortSome(a,b))
var a = [100,200,300]
var b = [{id:'100',name:'小红'},{id:'300',name:'小明'},{id:'200',name:'小蓝'}]
var obj={};
b.forEach((x)=>{
obj[x.id]=x
})
var arr=a.map((x)=>{
return obj[x]
})
console.log(arr)
Only loop once: