0

Here is the source code

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" },
  ])
);

When I iterate the array using normal for loop the code works fine! The error occurs whenever i try to call the getKeyFields method.

  • 2
    getKeyFields isn't passed an object with key array but an array. You should use `jsonObject.forEach((element) => {` – Shubham Khatri Jan 11 '21 at 09:13
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Ivar Jan 11 '21 at 09:15

0 Answers0