Can someone please help me fix this function? It will work if I change the two variables to integers (ie: '987') but if I change them to a character value like 'test' it won't search the array.
I'm new to javascript so excuse me if this is an easy fix. Thank you in advance.
var myArray = [{
'Vendor': '123',
'Item': '987',
'ID': '1'
}, {
'Vendor': '123',
'Item': '654',
'ID': '2'
}];
function findById(source, Vendor, Item) {
return source.filter(function(obj) {
return +obj.Vendor === +Vendor, +obj.Item === +Item;
})[0];
}
var vendin = '123';
var prodin = '654';
var result = findById(myArray, vendin, prodin);
console.log(result.ID);
The variables in question are vendin and prodin.
returnstatement. Didn't you mean to usereturn +obj.Vendor === +Vendor && +obj.Item === +Item;?