为什么 forEach 函数在此处给出错误“无法读取未定义的属性 'forEach'”?

问题描述

这是源代码

function MetadataParser(version,channel,keyField) {
  let _version = version;
  let _channel = channel;
  let _keyField = keyField;
  this.setVersion = function (version) {
    _version = version;
  };
  this.getVersion = function () {
    _version = version;
  };
  this.setChannel = function (channel) {
    _channel = channel;
  };
  this.getChannel = function () {
    return _channel;
  };
  this.setKeyField = function (keyField) {
    _keyField = keyField;
  };
  this.getVersion = function () {
    return _keyField;
  };
  this.getKeyFields = function (jsonObject) {
    let keyField = [];
    jsonObject.array.forEach((element) => {
      keyField.push(element.channel);
    });
    return keyField;
  };
} 
const MetadataParser = new MetadataParser("A","B","C");
console.log(
  MetadataParser.getKeyFields([
    { channel: "A" },{ channel: "B" },{ channel: "C" },])
);

当我使用普通 for 循环迭代数组时,代码工作正常!每当我尝试调用 getKeyFields 方法时都会发生错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)