3

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);
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Max Groh
  • 31
  • 1
  • Obviously you can do what you like in your own code, but when sharing code with others (for instance, asking for help), please take the time to indent and format your code in a readable, consistent way. (It's a good idea even when not sharing code with others, too.) (I've taken the code, run it through http://jsbeautifier.org [no affiliation], and removed extraneous blank lines for you on this occasion. [Ah, but now Fran's undone that, but done something similar.]) – T.J. Crowder Nov 13 '17 at 14:38
  • 2
    `for(var i = 0; i<= carray.length; i++){` You are going one past the end of the array. – 001 Nov 13 '17 at 14:39
  • Was formatting the code and question at the same time, I must have hit save just after you. Sorry @T.J.Crowder – Nope Nov 13 '17 at 14:42
  • @Fran: No worries, we were doing much the same thing. – T.J. Crowder Nov 13 '17 at 14:44

2 Answers2

0

Well you can trim all that down to this. And the check to see if the systemId is an array I referenced this answer

var filteredArray = CoilArray.filter(function (obj) {
  let isSystemA = false;
  let isStrB = false;

  // If Array then check if Array has value
  if (obj.systemId.constructor == Array) {
      isSystemA = obj.systemId.includes("System A") && isStrB;
  } else {
      isSystemA = obj.systemId === "System A";
  }

  if (obj.fieldStr.constructor == Array) {
      isStrB = obj.fieldStr.includes("Str B") && isStrB;
  } else {
      isStrB = obj.fieldStr === "Str B";
  }

  return isSystemA && isStrB;
});

This uses the filter function

João Cunha
  • 9,929
  • 4
  • 40
  • 61
  • Thank you for your answer. I tried to solve via `filter()` but it won't work for multiple object values of one key that are defined in `CoilArray` (e.g. `fieldStr: ["Str B", "Str C"],`). It will only be able to filter the third object, since both keys I am looking for have only one value. I believe my code above works, but I do not know why `obj` in `hasValue()` is statet as `undefined`. Can you help me with this? – Max Groh Nov 16 '17 at 06:53
  • @MaxGroh I'm gonna adjust my answer – João Cunha Nov 16 '17 at 08:55
0

Your for-loop runs from 0 to carray.length. As a consequence, the last time i is called, it has a value of carray.length. And carray[carray.length] doesn't exist.

Just stop looping at carray.length - 1 and your code works as expected.

Demo

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);
John Slegers
  • 45,213
  • 22
  • 199
  • 169