微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么 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 举报,一经查实,本站将立刻删除。