Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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));
share|improve this question
3  
What have you tried? – Frits van Campen May 10 '12 at 19:43
1  
possible duplicate of Remove duplicates from an array of objects in javascript – Royi Namir May 10 '12 at 19:46
2  
Not all such question deserve What have you tried. It is legitimate question not a spam. – Sarfraz May 10 '12 at 19:47
1  
@Sarfraz lol ... you right :)........(ps , what have you tried ?) – Royi Namir May 10 '12 at 19:48
And why should someone else do this work for you? – Travis J May 10 '12 at 19:50
show 3 more comments

2 Answers

up vote 8 down vote accepted

For example:

counter = {}

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

Docs: Array.forEach, JSON.stringify.

share|improve this answer
I don't believe there's a forEach function on arrays. – Elliot Bonneville May 10 '12 at 19:59
2  
@ElliotBonneville: but you do believe in google, don't you – thg435 May 10 '12 at 20:05
Wow, I can't believe I've never heard of that function! I know what I'm doing instead of 8 zillion for loops, now. Thanks! – Elliot Bonneville May 10 '12 at 20:08
@thg435 This is superb! I learnt a lot here, thanks very much. – Ben May 10 '12 at 20:09
@ElliotBonneville: not at all, I added doc links to the post for others who are curious. – thg435 May 10 '12 at 20:11
show 4 more comments
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]);
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.