2

I've looked through many stack overflow questions, but none seem to quite answer my question. I have an array of objects, which I would like to reduce by deleting all objects where the key and value are the same.

So my array of objects would be:

[{a:1},{a:2},{c:3},{b:1},{a:1},{c:3},{c:4},{a:1}]

The end result should be:

[{a:1},{a:2},{c:3},{b:1},{c:4}]

I've tried using filer and map, but I can only get the first object in the array, rather than all the objects that have different key/value pairs in the array. I've also tried using filter and findIndex, but with the same problem.

I also can't filter the objects before pushing them into the array.

Can someone point me in the right direction?

7
  • 2
    as it looks, all objects are unique. Commented Jun 28, 2018 at 17:53
  • 1
    how so? in the original array, there are three objects that are both {a:1}, as well as two that are {c:3} Commented Jun 28, 2018 at 17:56
  • @Teemu I looked at that one too, but the solution using filter and findIndex only returns the first instance of a unique object Commented Jun 28, 2018 at 17:57
  • @flored27, objects containing the same keys and values are not equal; only references to the same object are equal. This is why {} !== {}. Commented Jun 28, 2018 at 17:57
  • @Teemu it looks like the accepted answer filters the objects before placing them into an array. However, I already have an array of objects, and can't be filtered beforehand Commented Jun 28, 2018 at 17:59

3 Answers 3

3

You can compare the two items using JSON.stringify(). We then add to a new array using reduce, if it is in the array we don't add it otherwise we do add it.

const array = [{a:1},{a:2},{c:3},{b:1},{a:1},{c:3},{c:4},{a:1}]

let unique = array.reduce((res, itm) => {
  // Test if the item is already in the new array
  let result = res.find(item => JSON.stringify(item) == JSON.stringify(itm))
  // If not lets add it
  if(!result) return res.concat(itm)
  // If it is just return what we already have
  return res
}, [])

console.log(unique)

Alternatively you could use a Set (as Fissure King metions) to make a unique list of items like this:

const array = [{a:1},{a:2},{c:3},{b:1},{a:1},{c:3},{c:4},{a:1}]

let unique = [...new Set(array.map(itm => JSON.stringify(itm)))].map(i => JSON.parse(i))

console.log(unique)

Sign up to request clarification or add additional context in comments.

5 Comments

Alternatively, but in the same vein: [...new Set(array.map((item) => JSON.stringify(item))]
Didn't think of a set! I like it!
I really like this answer! Any way you can explain to me why the ...new?
Because Set is a class, and we need to make an instance of that class.
if you mean the ... that is Spread Syntax
0

Assuming that all your object are of different types(different properites) and are not complex in nature i.e., not nested objects..

  1. Create a array list(which will act as multi dimensional array).

    let uniqueArr = [];

  2. Loop through your array which contains duplicates with Arr.forEach();

  3. Get property of the object using

    Object.getPropertyNames(item);

  4. Check wether this type of object type already exists in your uniqueArr ,if not add the property type.

uniqueArr.push({property:[]});

  1. If the property already exists in the uniqueArr, check whether the current property value exits in the property array inside uniqueArr.
  2. If the property doesn't not exist add the new property to the respective property array.if the property exits skip and run the loop for next object.
  3. Once loop completed, create resultArr with the help of the uniqueArr.

Sample : uniqueArr [ {a:[1,2]}, {b:[1]}, {c:[3]} ];

Comments

-1
var a = [{a:1},{a:2},{c:3},{b:1},{a:1},{c:3},{c:4},{a:1}];
var newData = [];
a.map(ele=>JSON.stringify(ele)).forEach(ele=>{
   if(newData.indexOf(ele) === -1){
     newData.push(ele)
   }
});
newData.map(ele=>JSON.parse(ele))
console.log(newData);

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.