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

Aram Grigoryan
  • 740
  • 1
  • 6
  • 24
kaka1234
  • 770
  • 3
  • 9
  • 30

1 Answers1

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);
    }
}));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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: https://jsfiddle.net/50pxjkda/7/ – kaka1234 Sep 19 '18 at 19:19
  • Please use [Stack Snippet](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) instead of jsfiddle. – Barmar Sep 19 '18 at 19:26
  • 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. – kaka1234 Sep 20 '18 at 17:19
  • 1
    Put an `if` statement in the `forEach` function. `array2.forEach(o => { if (o.table != 'table1') { obj[o.table] = o; } })` – Barmar Sep 20 '18 at 17:23
  • Works fine. Thanks for your time. – kaka1234 Sep 20 '18 at 17:52