Currently, I am attempting to compare each item in two arrays to find matches. At the moment, I am only comparing one attribute, but I aim to expand that to two once I resolve this issue.
I'm puzzled by the fact that it successfully processes the first three items in the array but encounters an error on the fourth one. Below is the Chrome console output:
Washington
Smith
yes
Jones
Uncaught TypeError: Cannot read property 'name' of undefined
Below is my JavaScript code snippet:
var self = this;
self.people = [
{ id: '1', name: 'Washington' },
{ id: '2', name: 'Smith' },
{ id: '1', name: 'Jones' },
{ id: '1', name: 'Smith' },
{ id: '3', name: 'Washington' }
];
self.params = [
{id: '1', name: 'Jones'},
{id: '2', name: 'Smith'}];
for (var val in self.params) {
for (var value in self.people) {
console.log(self.people[value].name);
if (self.people[value].name == self.params[value].name) {
console.log('yes');
}
}
}
If I eliminate the if statement, the code functions without errors and outputs the "names" in the people array twice as expected. Any ideas? Thank you in advance!