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
: