JSFiddle - React, Tailwind, and code Playground

by anup1986

HTML

<div id="data" />

JavaScript

var testObj = {
  "metadata": {
    "endUser": [],
    "language": "sv-SE",
    "targetAudienceLanguage": "",
    "vocabularyForeignLanguage": "",
    "vocabularyLocalLanguage": "",
    "isSelfAssessment": false,
    "discipline": "",
    "year": []
  },
  "attributes": {
    "outputclass": {
      "local": "openingImage",
      "inherited": "section",
      "aggregated": [
        "product",
        "chapter",
        "section"
      ]
    }
  }
};

const getField = (fieldsArray) => (obj) => fieldsArray.reduce((c, prop) => c[prop], obj);

var div = document.getElementById('data');
div.innerText = getField(['attributes', 'outputclass', 'else'])(testObj);