json(オブジェクト配列である属性の値に基づいて重複するデータを取得)

1246 ワード

let arr = [
	        {id: 1, sku: 101, name: 'sku101'},
	        {id: 2, sku: 102, name: 'sku102'},
	        {id: 3, sku: 103, name: 'sku103'},
	        {id: 4, sku: 101, name: 'sku101'},
	        {id: 5, sku: 102, name: 'sku102'},
	        {id: 6, sku: 103, name: 'sku103'},
	        {id: 7, sku: 104, name: 'sku104'},
	        {id: 8, sku: 105, name: 'sku105'},
	      ]

 /**
	     *                     
	     * @param {*} data      
	     */
	    getRepeatData(data) {
	      let result=[];
	      let obj = {};
	        for (let i = 0; i < data.length; i++) {
	          const el = data[i];
	          for (let y = i+1; y < data.length; y++) {
	            const val = data[y];
                 //      sku  
	            if( el.sku == val.sku ) {
	              if(!obj[el.id]){
	                result.push(el)
                    //         id,     ,  ,        true,    push
	                obj[el.id] = true
	              }
	              if(!obj[val.id]){
	                result.push(val)
	                obj[val.id] = true
	              }
	            }
	          }
	        }
	      return result
	  }

	  getRepeatData(arr)