Javascript – Sortieren Sie Objekte basierend auf den Werten im Array
我想大声告诉你
我想大声告诉你 2017-05-19 10:46:17
0
5
561

var a = [100.200.300]

var b = [{id:'100',name:'Xiaohong'},{id:'300',name:'Xiaoming'},{id:'200',name:'Xiaolan'}]

我想大声告诉你
我想大声告诉你

Antworte allen(5)
左手右手慢动作
var a = [100,200,300];
var b = [{id:'100',name:'小红'},{id:'300',name:'小明'},{id:'200',name:'小蓝'}];

var result = b.sort((aa, bb) => (a.indexOf(aa.id - 0) - a.indexOf(bb.id - 0)));

// 不考虑重复ID的话方法还有很多:
result = a.map(el_a=>(b.find(el_b => el_b.id == el_a)));
result = b.reduce((acc,cur) => (acc[a.indexOf(cur.id - 0)] = cur, acc), []);
仅有的幸福
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))
洪涛
let c=[];

for(let num of a){
    b.forEach(item => {
        if(item.id == num) c.push(item)
    })
}
曾经蜡笔没有小新
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)
小葫芦

只循环一次:

b.forEach(function(ele, index, arr) {
    if (a.indexOf(parseInt(ele.id)) > -1)
        this[a.indexOf(parseInt(ele.id))] = ele;
}, out = [])
console.log(JSON.stringify(out));
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage