Reputation: 6242
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
Upvotes: 2
Views: 2411
Reputation: 781058
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);
}
}));
Upvotes: 1