12

I have an array of objects as follows within my server side JS:

[
    {
        "Company": "IBM"
    },
    {
        "Person": "ACORD LOMA"
    },
    {
        "Company": "IBM"
    },
    {
        "Company": "MSFT"
    },
    {
        "Place": "New York"
    }
]

I need to iterate through this structure, detect any duplicates and then create a count of a duplicate is found along side each value.

Both of the values must match to qualify as a duplicate e.g. "Company": "IBM" is not a match for "Company": "MSFT".

I have the options of changing the inbound array of objects if needed. I would like the output to be an object, but am really struggling to get this to work.

EDIT: Here is the code I have so far where processArray is the array as listed above.

var returnObj = {};

    for(var x=0; x < processArray.length; x++){

        //Check if we already have the array item as a key in the return obj
        returnObj[processArray[x]] = returnObj[processArray[x]] || processArray[x].toString();

        // Setup the count field
        returnObj[processArray[x]].count = returnObj[processArray[x]].count || 1;

        // Increment the count
        returnObj[processArray[x]].count = returnObj[processArray[x]].count + 1;

    }
    console.log('====================' + JSON.stringify(returnObj));
isherwood
  • 58,414
  • 16
  • 114
  • 157
Ben
  • 6,026
  • 11
  • 51
  • 72
  • 1
    possible duplicate of [Remove duplicates from an array of objects in javascript](http://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Royi Namir May 10 '12 at 19:46
  • And **why** should someone else do this work for you? – Travis J May 10 '12 at 19:50
  • @RoyiNamir: hahaha it is not me :) – Sarfraz May 10 '12 at 19:50
  • Which *both* values are you talking about? Each object has only one value... and if you already know that you have to iterate over list, what are you having problems with? – Felix Kling May 10 '12 at 19:53
  • Apologies all for not posting my testing code. Please see the additional edit. – Ben May 10 '12 at 20:02

4 Answers4

37

For example:

counter = {}

yourArray.forEach(function(obj) {
    var key = JSON.stringify(obj)
    counter[key] = (counter[key] || 0) + 1
})

Docs: Array.forEach, JSON.stringify.

georg
  • 211,518
  • 52
  • 313
  • 390
2
Object.prototype.equals = function(o){
    for(var key in o)
        if(o.hasOwnProperty(key) && this.hasOwnProperty(key))
            if(this[key] != o[key])
                return false;
    return true;
}
var array = [/*initial array*/],
    newArray = [],
    ok = true;
for(var i=0,l=array.length-1;i<l;i++)
    for(var j=i;j<l+1;j++)
    {
       if(!array[i].equals(array[j]))
           newArray.push(array[i]);
    }
gion_13
  • 41,171
  • 10
  • 96
  • 108
1

We are required to write a JavaScript function that takes in one such array of objects. The function creates and return a new array in which no objects are repeated (by repeated we mean objects having same value for "Country" property.)

Moreover, the function should assign a count property to each object that represents the number of times they appeared in the original array.

const arr = [
   {
      "Country": "BR",
      "New Lv1−Lv2": "#N/A"
   },
   {
      "Country": "BR",
      "New Lv1−Lv2": "#N/A"
   },
   {
      "Country": "",
      "New Lv1−Lv2": "test"
   }];
   const convert = (arr) => {
      const res = {};
      arr.forEach((obj) => {
         const key = `${obj.Country}${obj["New Lv1−Lv2"]}`;
         if (!res[key]) {
            res[key] = { ...obj, count: 0 };
         };
         res[key].count += 1;
      });
   return Object.values(res);
};
console.log(convert(arr));

know more

MD SHAYON
  • 7,001
  • 45
  • 38
0

With ES6, one can use Array#reduce with an object to store the counts.

let counts = arr.reduce((acc, curr)=>{
   const str = JSON.stringify(curr);
   acc[str] = (acc[str] || 0) + 1;
   return acc;
}, {});

Demo

To create a new array without duplicates, a Set can be used with Array#filter.

let set = new Set;
let res = arr.filter(x => {
  const str = JSON.stringify(x);
  return !set.has(str) && set.add(str);
});

Demo

Unmitigated
  • 76,500
  • 11
  • 62
  • 80