Encapsulation function to remove duplicate elements with the same id in the array object

 var arr = [
	 {
    
    id:1,name:'aa'},
	 {
    
    id:1,name:'aa'},
	 {
    
    id:3,name:'cc'},
	 {
    
    id:4,name:'dd'}
 ] 
        //考察点: 对js基础知识的掌握
        //解法一:
        //数组各种方法的应用
  function deWeight(arr,initArr=[]) {
    
    
      arr.forEach(item=>{
    
    
          let isFind = initArr.find(cell=> cell.id === item.id)
          if(!isFind) {
    
    
              initArr.push(item)
          }
      })
      return initArr
  } 

Scan the code to get 1000+ front-end interview questions collection to try later

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42981560/article/details/109049276