首页 > 技术文章 > Js数组includes()使用方式 该方法返回一个布尔值,表示某个数组是否包含给定的值

yangxuanxuan 2020-06-10 11:10 原文

[1, 2, 3].includes(2); // true
 
[1, 2, 3].includes(4); // false
 
[1, 2, NaN].includes(NaN); // true

没有该方法之前,我们通常使用数组的indexOf方法,检查是否包含某个值。
if (arr.indexOf(el) !== -1) {
 
// ...
 
}


若是 后台返回  数组的形式

此处使用的是angular的写入

const arr = data.operationHistoryInfoList;
const sameTimeArr = [];
arr.forEarch(v=>{
    if(!sameTimeArr.includes(v.operTime)){
     sameTimeArr.push(v.operTime)
    }

})

const newArr =[];
sameTimeArr.forEarch(item =>{
 const sameArr = arr.filter(v=>item === v.operTime){
    newArr.push({
      operTime:item,
      operTimeSameArr:sameArr 

   })

 }
})        

  

推荐阅读