I have two arrays where I need to identify matching values across different sequences. How can I achieve this comparison using plain JavaScript? Specifically, I am trying to match the values of property1 and name and then extract property3 when property1 matches name.
Here is the code snippet:
var data = [];
var data1 = [
{'property1': 'john', 'property2': 12},
{'property1': 'jasmin', 'property2': 22},
{'property1': 'dog', 'property2': 22}
];
var data2 = [
{'name': 'dog', 'property2': 12, 'property3': 'xys'},
{'name': 'john', 'property2': 22, 'property3': 'acb'},
{'name': 'jasmin', 'property2': 22, 'property3': 'jjj'}
];
for(var i=0; i<data1.length; i++){
if(data1[i].property1 == data2[i].name){
data.push({
'property1': data1[i].property1,
'property2': data1[i].property2,
'property3': data2[i].property3
});
} else {
console.log('not equal');
}
}
The jsfiddle link for reference
The expected output should be:
data=[{'property1': 'john', 'property2': 12, 'property3': 'acb'},
{'property1': 'jasmin', 'property2': 22, 'property3': 'jjj'},
{'property1': 'dog', 'property2': 22, 'property3': 'xys'}]