How to get duplicate objects in an array by JS

for example, there is an array

let arr = [
    { "name": "" , "value" : "2", "type" : "1"}
    { "name": "" , "value" : "3", "type" : "1"}
    { "name": "" , "value" : "3", "type" : "1"}
    { "name": "" , "value" : "2", "type" : "1"}
    { "name": "" , "value" : "4", "type" : "1"}
]

now you need to take out both the duplicate name object and the duplicate value object. You can take the subscript or the whole object.
if the name of 1 and 5 is the same, return the first, the value of 2 and 3 is the same, and return the second. How to solve

?
May.04,2022

let arr = [
  { name: '', value: '2', type: '1' },
  { name: '', value: '3', type: '1' },
  { name: '', value: '3', type: '1' },
  { name: '', value: '2', type: '1' },
  { name: '', value: '4', type: '1' },
  { name: '', value: '5', type: '1' }
];
let arrD = [];
const map = new Map();
arr.forEach(v => {
  if (map.get(v.name) && arrD.every(vD => vD.name != v.name)) {
    arrD.push(v);
  } else {
    map.set(v.name, v);
  }
}); // arrD = [{name: "", value: "4", type: "1"}]

filter will do


  let arr = [
    {'name': '' , 'value' : '2', 'type' : '1'},
    {'name': '' , 'value' : '3', 'type' : '1'},
    {'name': '' , 'value' : '3', 'type' : '1'},
    {'name': '' , 'value' : '2', 'type' : '1'},
    {'name': '' , 'value' : '4', 'type' : '1'}
  ];

  var aa= arr.filter(function(p){return p.name== '';})[0];
  console.log(aa);    //{name: "", value: "2", type: "1"}

  var bb= arr.filter(function(p){return p.value== '3';})[1];
  console.log(bb);    //{name: "", value: "3", type: "1"}

let arr = [
  {'name': '', 'value': '2', 'type': '1'},
  {'name': '', 'value': '3', 'type': '1'},
  {'name': '', 'value': '3', 'type': '1'},
  {'name': '', 'value': '2', 'type': '1'},
  {'name': '', 'value': '4', 'type': '1'},
  {'name': '', 'value': '4', 'type': '1'}
]
let resMap = {}
for (let i = 0; i < arr.length; iPP) {
  let outItem = arr[i]
  if (resMap[outItem.name]) {
    resMap[outItem.name].countPP
    continue
  }
  for (let j = i + 1; j < arr.length; jPP) {
    let innerItem = arr[j]
    if (outItem.name === innerItem.name) {
      resMap[outItem.name] = {
        value: outItem,
        count: 1
      }
      break
    }
  }
}
console.log(resMap) // {"":{"value":{"name":"","value":"2","type":"1"},"count":3},"":{"value":{"name":"","value":"3","type":"1"},"count":2}}

 let arr = [
        { 'name': '' , 'value' : '2', 'type' : '1'},
        { 'name': '' , 'value' : '3', 'type' : '1'},
        { 'name': '' , 'value' : '3', 'type' : '1'},
        { 'name': '' , 'value' : '2', 'type' : '1'},
        { 'name': '' , 'value' : '4', 'type' : '1'}]

      let newarr = arr.filter((item, index, self)=>{
          const obj = {}
          for(let i = index+1; i < self.length; iPP){
             if(self[i].name === item.name || self[i].value === item.value){
                return true
             }
          }

          return false
      })

      console.log(newarr)

idea : this kind of problem belongs to the category of data deduplication, so just redo it according to the data.

effect :

clipboard.png

Code :

    let arr = [
        { 'name': '' , 'value' : '2', 'type' : '1'},
        { 'name': '' , 'value' : '3', 'type' : '1'},
        { 'name': '' , 'value' : '3', 'type' : '1'},
        { 'name': '' , 'value' : '2', 'type' : '1'},
        { 'name': '' , 'value' : '4', 'type' : '1'},
    ];

    const dataGroup = (dataSource, splitFields) => {
        // 
        return splitFields.reduce( (data, field) => {
            // field
            data[field] = dataSource.reduce((dataInner, item) => {
                dataInner[item[field]] = dataInner[item[field]] || item;
                return dataInner;
            }, {})
            return data;
        }, {})
    }

    console.log(  dataGroup(arr, ['name', 'value'])  );

Note: if it is not the result you want, you can explain it in the comments.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3f5f4-2c483.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3f5f4-2c483.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?