0

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"
}]

4 Answers 4

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);

1 Comment

what does this have to do with the OP's question?
0

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);

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.