Home > database >  Show ID of Duplicate Values in Array of Object
Show ID of Duplicate Values in Array of Object

Time:12-13

I found here this code and works good. But I also need to know the "id" which is multible in use. e.g. in another array

var data = [
  {"id":"1","Group":"Wohnzimmer","Light":"Diele", "type":"ct"},
  {"id":"1","Group":"Wohnzimmer","Light":"Diele", "type":"ct"},
  {"id":"2","Group":"Wohnzimmer","Light":"Diele", "type":"bri"},
  {"id":"3","Group":"Wohnzimmer","Light":"Diele", "type":"color"},
  {"id":"3","Group":"Wohnzimmer","Light":"Diele", "type":"color"},
]

var a = data.reduce((accumulator, current) => {
  if (checkIfAlreadyExist(current)) {
    return accumulator;
  } else {
    return [...accumulator, current];
  }

  function checkIfAlreadyExist(currentVal) {
    return accumulator.some((item) => {
      return (item.id === currentVal.id &&
              item.Light === currentVal.Light &&
              item.type === currentVal.type);
    });
  }
}, []);
            
console.log(a);

Reduced (it works fine!):

[{
  Group: "Wohnzimmer",
  id: "1",
  Light: "Diele",
  type: "ct"
}, {
  Group: "Wohnzimmer",
  id: "2",
  Light: "Diele",
  type: "bri"
}, {
  Group: "Wohnzimmer",
  id: "3",
  Light: "Diele",
  type: "color"
}]

Now, I need also the result of the deleted objects, like the following:

[{
  Group: "Wohnzimmer",
  id: "1",
  Light: "Diele",
  type: "ct"
},{
  Group: "Wohnzimmer",
  id: "3",
  Light: "Diele",
  type: "color"
}]

CodePudding user response:

use array.prototype.map and array.prototype.some

var values = [
    { name: 'someName1' },
    { name: 'someName2' },
    { name: 'someName4' },
    { name: 'someName2' }
];

var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){ 
    return valueArr.indexOf(item) != idx 
});
console.log(isDuplicate);

CodePudding user response:

Using you current logic you could archive your preferred output with small changes, use a object instead with two arrays instead.

var data = [
  { id: "1", Group: "Wohnzimmer", Light: "Diele", type: "ct" },
  { id: "1", Group: "Wohnzimmer", Light: "Diele", type: "ct" },
  { id: "2", Group: "Wohnzimmer", Light: "Diele", type: "bri" },
  { id: "3", Group: "Wohnzimmer", Light: "Diele", type: "color" },
  { id: "3", Group: "Wohnzimmer", Light: "Diele", type: "color" },
];

var { unique, removed } = data.reduce(
  (accumulator, current) => {
    if (checkIfAlreadyExist(current)) {
      return {
        ...accumulator,
        removed: [...accumulator.removed, current],
      };
    } else {
      return {
        ...accumulator,
        unique: [...accumulator.unique, current],
      };
    }
    function checkIfAlreadyExist(currentVal) {
      return accumulator.unique.some((item) => {
        return (
          item.id === currentVal.id &&
          item.Light === currentVal.Light &&
          item.type === currentVal.type
        );
      });
    }
  },
  {
    unique: [],
    removed: [],
  }
);

console.log("Unique");
console.log(unique);
console.log("Removed");
console.log(removed);

CodePudding user response:

You can do this efficiently in linear time:

let key = obj => [obj.id, obj.Light, obj.type].join('@@')

let seen = new Set, 
    unique = [], 
    removed = []

for (let obj of data) {
    let k = key(obj);
    (seen.has(k) ? removed : unique).push(obj)
    seen.add(k)
}

CodePudding user response:

Just create another array to store deleted items and if checkIfAlreadyExist returns true push current into the array.

var data = [
  {"id":"1","Group":"Wohnzimmer","Light":"Diele", "type":"ct"},
  {"id":"1","Group":"Wohnzimmer","Light":"Diele", "type":"ct"},
  {"id":"2","Group":"Wohnzimmer","Light":"Diele", "type":"bri"},
  {"id":"3","Group":"Wohnzimmer","Light":"Diele", "type":"color"},
  {"id":"3","Group":"Wohnzimmer","Light":"Diele", "type":"color"},
]

var deleted = []

var a = data.reduce((accumulator, current) => {
  if (checkIfAlreadyExist(current)) {
    deleted.push(current)
    return accumulator;
  } else {
    return [...accumulator, current];
  }

  function checkIfAlreadyExist(currentVal) {
    return accumulator.some((item) => {
      return (item.id === currentVal.id &&
              item.Light === currentVal.Light &&
              item.type === currentVal.type);
    });
  }
}, []);
            
console.log(a);
console.log(deleted)

  • Related