简体   繁体   English

function中的Object是undefined js

[英]Object in function is undefined js

I am trying to find out whether an object of my Object-Array CoilArray contains a certain value.我试图找出我的对象数组CoilArray的 object 是否包含某个值。

For this I am using the function hasValue为此,我使用 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我希望能够过滤两个值并返回过滤后的数组,如您在 function filterArray中所见

I have a problem in function hasValue .我在 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. obj显然应该是我给 function 的 object。

Why is it undefined?为什么它是未定义的?
How can I define it?我该如何定义它?

hasValue itself seems to be working though hasValue本身似乎在工作

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);

Well you can trim all that down to this. 那么你可以减少这一切。 And the check to see if the systemId is an array I referenced this answer 并检查systemId是否是我引用此答案的数组

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 这使用了过滤功能

Your for-loop runs from 0 to carray.length .您的 for 循环从 0 运行到carray.length As a consequence, the last time i is called, it has a value of carray.length .因此,最后一次调用i时,它的值为carray.length And carray[carray.length] doesn't exist.而且carray[carray.length]不存在。

Just stop looping at carray.length - 1 and your code works as expected.只需在carray.length - 1处停止循环,您的代码就会按预期工作。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM