JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
</head>
<body>
<div id="console">
<h2>Console</h2>
</div>
</body>
</html
CSS
div {
border: 1px solid black;
}
h2 {
font-size: 2em;
}
JavaScript
var data = [
[
{"name":"product1","light":"1"},
{"name":"product2","light":"2"},
{"name":"product5","light":"5"},
{"name":"product4","light":"4"}
],
[
{"name":"product2","light":"2"},
{"name":"product3","light":"3"},
{"name":"product4","light":"4"}
]
];
$(function() {
var intersection = valueIntersect(data[0],data[1]);
for (o in intersection) {
$("#console").append("<p>Name: " + intersection[o].name + ", Light: " + intersection[o].light + "</p>");
}
});
var list1 = [ {userId:1234,userName:'XYZ'},
{userId:1235,userName:'ABC'},
{userId:1236,userName:'IJKL'},
{userId:1237,userName:'WXYZ'},
{userId:1238,userName:'LMNO'}
];
var list2 = [ {userId:1235,userName:'ABC'},
{userId:1236,userName:'IJKL'},
{userId:1252,userName:'AAAA'}
];
function operation(list1, list2, operationIsUnion) {
var result = [];
for (var i = 0; i < list1.length; i++) {
var item1 = list1[i],
found = false;
for (var j = 0; j < list2.length; j++) {
if (item1.userId === list2[j].userId) {
found = true;
break;
}
}
if (found === operationIsUnion) {
result.push(item1);
}
}
return result;
}
// Following functions are to be used:
function inBoth(list1, list2) {
return operation(list1, list2, true);
}
function valueIntersect(a1, a2) {
var aReturn = [];
for(i1 in a1) {
o1 = a1[i1];
for (i2 in a2) {
o2 = a2[i2];
if(equals(o1,o2)) {
aReturn.push(o1);
break;
}
}
}
return aReturn;
}
function equals(o1, o2) {
if (!o2 || o1.length != o2.length) {
return false;
}
for (i in o1) {
if (o1[i] !== o2[i]) {
return false;
}
}
return true;
};