I am trying to find out whether an object of my Object-Array CoilArray
contains a certain value.
For this I am using the function hasValue
I want to be able to filter on two values and give back a filtered array, as you can see in the function filterArray
I have a problem in function hasValue
.
The error message I am getting says:
obj is undefined
obj
is obviously supposed to be the object that I am giving to the function.
Why is it undefined?
How can I define it?
hasValue
itself seems to be working though
var CoilArray = [{
id: "First Coil",
systemId: ["System A", "System C"],
fieldStr: ["Str A", "Str B"],
}, {
id: "Second Coil",
systemId: "System B",
fieldStr: ["Str B", "Str C"],
}, {
id: "Third Coil",
systemId: "System C",
fieldStr: "Str C",
}, ]
function hasValue(obj, key, value) {
if (obj[key].indexOf(value) > -1) {
return true;
}
}
function filterArray(carray) {
var arr = new Array();
for (var i = 0; i <= carray.length; i++) {
if (hasValue(carray[i], "systemId", "System A") &&
hasValue(carray[i], "fieldStr", "Str B")) {
arr.push(carray[i].id);
console.log(carray[i].id);
}
}
return arr;
}
hasValue(CoilArray[0], "fieldStr", "Str A");
filterArray(CoilArray);