2

I have a json array1 as:

[
 { id:0, name:"adam", uName: "aSilver", uId: "123", table: "table1"},
 { id:1, name:"john", uName: "jBerlin", uId: "456", table: "table1"}
]

I have another json array2 as:

[
 { id:0, name:"adam", uName: "aSilver, jBerlin", uId: "123, 456", createdBy: 
"auto", createdOn: "09/10/2018", desc: "none", table: "table1"},
 { id:1, name:"john", uName: "aSilver, jBerlin", uId: "123, 456", createdBy: 
"auto", createdOn: "09/10/2018", desc: "none1", table: "table1"},
 { id:0, name:"steve" uName: "aSilver, jBerlin, pParis", uId: "123, 456, 
 789", createdBy: "auto", createdOn: "09/10/2018", desc: "none2", table: 
"table2"},
 { id:0, name:"nash", uName: "aSilver, jBerlin, pParis", uId: "123, 456, 
 789", createdBy: "auto", createdOn: "09/10/2018", desc: "none3", table: 
"table2"},
 { id:0, name:"sand", uName: "aSilver", uId: "123", createdBy: "auto", 
createdOn: "09/10/2018", desc: "none4", table: "table3"}
]

I have to check if the combination of uname+uid in array1 exists in array2

So as below:

From array2 first I have to get only the unique values. Unique values are based on "table" key. Resulting in below:

[
 { id:0, name:"adam", uName: "aSilver, jBerlin", uId: "123, 456", createdBy: 
"auto", createdOn: "09/10/2018", desc: "none", table: "table1"},
 { id:0, name:"steve" uName: "aSilver, jBerlin, pParis", uId: "123, 456, 
789", createdBy: "auto", createdOn: "09/10/2018", desc: "none2", table: 
"table2"},
 { id:0, name:"sand", uName: "aSilver", uId: "123", createdBy: "auto", 
createdOn: "09/10/2018", desc: "none4", table: "table3"}
]

Now I may loop through above array creating new json array3 as:

[
 { comparer: "aSilver_123, jBerlin_456", table: "table1" }
 { comparer: "aSilver_456, jBerlin_456, pParis_789", table: "table 2" }
 { comparer: "aSilver_123", table: "table 3" }
]

Now I need to compare array1 & array3 comparer key only

Since the array1 has "aSilver_123, jBerlin_456" which is similar to the first item of array3. I would now throw an error that "table1" has duplicate values.

For now, I found below method that checks for uniqueness but I have to find uniqueness in an object array so this won't work.

     Array.prototype.unique = function () {
        var r = new Array();
        o: for (var i = 0, n = this.length; i < n; i++) {
            for (var x = 0, y = r.length; x < y; x++) {
                if (r[x] == this[i]) {                       
                    return false;
                }
            }
            r[r.length] = this[i];
        }
        return true;
    }

Sorry, I know its a little unusual comparison which I am looking for.

Wanted to know what the efficient way to approach this?

Thanks

Here is my jsfiddle:

http://jsfiddle.net/50pxjkda/7

2
  • 1
    None of that is JSON Commented Sep 19, 2018 at 19:22
  • Does this answer your question? How to compare arrays in JavaScript? Commented Jul 5, 2022 at 10:41

1 Answer 1

1

Create an object whose keys are the table properties of array2. Since object keys are unique, this will get you the unique objects.

obj = {};
array2.forEach(o => obj[o.table] = o);
unique_array2 = Object.keys(obj).map(k => obj[k]);

To find the elements common to array1 and unique_array2 you simply use nested loops that compare the properties.

matches = [];
array1.forEach(o1 => unique_array2.forEach(o2 => {
    if (o1.uName == o2.uName && o1.uid == o2.uid) {
        matches.push(o1);
    }
}));
Sign up to request clarification or add additional context in comments.

5 Comments

The above code works fine and gives me unique values from Array2. Now how shall I go ahead and compare with my array 1. In your code unique_array2 returns me array as obj is. I have created a jsfiddle for you: jsfiddle.net/50pxjkda/7
Please use Stack Snippet instead of jsfiddle.
I have modified the above to fit my use and it works fine. One last question, in the unique array which now has 3 rows for table1, table2, table3. How can I also add extra condition so as to filter out table1 and just show table2, table3. I can do like result = unique_array2.filter(data=> data.table != 'table1'); but was wondering if I can add this condition within existing code that you have shared.
Put an if statement in the forEach function. array2.forEach(o => { if (o.table != 'table1') { obj[o.table] = o; } })
Works fine. Thanks for your time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.