前端JS使用双重遍历根据key对应value的形式对数组排序

在这里插入图片描述

   let arr=[];
   let numberList=[1,2,3,4];
   let objList=[{
    
    name:'成都',value:2},{
    
    name:'上海',value:3},     {
    
    name:'北京',value:1},{
    
    name:'广州',value:4}];
   for(let i=0;i<numberList.length;i++){
    
    
      let code=numberList[i]
       for(let n=0;n<objList.length;n++){
    
    
              if(code==objList[n].value){
    
    
                    arr.push(objList[n])
              }
       }
}
console.log(arr)

猜你喜欢

转载自blog.csdn.net/m0_46577631/article/details/127917860