js找出数组中相同的元素放在一起

var test= [
        {
        
          applytime: 1637993230077,
       
        },
        {
         
          applytime: 1637993230077,
        
        },
        {
        
          applytime: 1637993230078,
        
        },
        {
         
          applytime: 1637993230078,
         
        },
      ]
     
      
 
 function   checkSameData(tableData2){
          let cache = {};  //存储的是键是applytime 的值,值是applytime 在indeces中数组的下标
          let indices = [];  //数组中每一个值是一个数组,数组中的每一个元素是原数组中相同applytime的下标
        
          tableData2.map((item,index)=>{
            let applytime = item.applytime;
            let _index = cache[applytime];

            if(_index!==undefined){
                 indices[_index].push(tableData2[index])
            }else{
                cache[applytime] = indices.length
                indices.push([ tableData2[index]])
            }
          })
         
      
              console.log(indices)
         
      }
 
 this.checkSameData(test)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容