0

Assuming that I have a JS object wich contains other objects inside it, but all of them have the same structure. I am trying to search inside them for any duplicated values. For example:

{
    {
        id: "123"
        name: "John"
        surname: "Smith"
        phone: "123456789"
    }, 
    {
        id: "456"
        name: "Jack"
        surname: "Jones"
        phone: "9876789123"
    }, 
    {
        id: "789"
        name: "John"
        surname: "Doe"
        phone: "123456789"
    }
}

I want to search and find that the property 'phone' with value '123456789' is the same on both objects with ids '123' and '789'.

Note: the property/value combination that I am searching for is unknown beforehand.

1

2 Answers 2

2

First, if the outer structure is a plain Object, use an Array instead. If you can't change the structure, I'd convert it first.

DEMO: http://jsfiddle.net/3Y2qr/

if (!Array.isArray(data))
    data = Object.keys(data)
                 .sort(function(a,b) { return a - b })
                 .map(function(key) { return data[key] })

Then you can reduce the set to groups of matches.

var groupByPhone = data.reduce(function(found, obj) {
    if (found[obj.phone])
        found[obj.phone].push(obj)
    else
        found[obj.phone] = [obj]
    return found
}, {});

var dupes = Object.keys(groupByPhone)
                  .filter(function(key) {
                      return groupByPhone[key].length > 1
                  })

So now groupByPhone is an object where the keys are the unique phone numbers, and the values are the objects that have that number.

The dupes will be a list of phone number keys that can be used to lookup groups of duplicate objects in groupByPhone.

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

1 Comment

Thank you very much, it is exactly what i was looking for. My main problem was that, as you figured out, the outer structure is an Object and i was manipulating it as an Array.
0

Array.prototype.filter to the rescue !

var searchFor = '123456789';

var filteredResult = arrayOfObjects.filter(function( obj ) {
    return obj.phone === searchFor;
});

console.log( filteredResult );

7 Comments

you can make it faster by avoiding a closure using this: return obj.phone==this;}, searchFor );
@dandavis It could also be a lot faster if a normal for loop is used
I think the idea is that the match string isn't known, and OP wants to find duplicates based on the .phone property. Also, I'm not certain that OP has an actual Array, though if not, one should be used.
@ian yes, usually for loops are still faster, though not as much faster as they were two years ago. Filter() provides code re-usability that for loops cannot touch, and that often out-weighs raw benchmark performance.
@squint you are right, the match string is unknown. I have tried with another combination of filter and i got an error that the object has no method filter
|

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.