js代码对对象数组进行排序,如果对象中的值为非数字式比如null或者“-“,排在最后


var mergedArray=[{name:"站点1",PM25 :10},{name:"站点1",PM25 :"-"},{name:"站点1",PM25 :15},{name:"站点1",PM25 :1}]
 mergedArray?.sort((a,b)=>{
    if (a === b) {
            return 0;
        }else if (a.PM25 == null || a.PM25 === "-") {
            return 1;
        }else if (b.PM25 == null || b.PM25 === "-") {
            return -1;
        } else { 
          // 降序
            return a.PM25 < b.PM25 ? 1 : -1;
        }
   })
 }
 
 //输出[{name:"站点1",PM25 :15},{name:"站点1",PM25 :10},{name:"站点1",PM25 :1},{name:"站点1",PM25 :"-"}]

你可能感兴趣的:(javascript,前端,java)