如何在JavaScript中访问嵌套数组内的属性?

时间:2019-04-03 23:22:18

标签: javascript arrays object multidimensional-array

我有一个像这样存储在JSON文件中的嵌套数组...

{
  "shabadinfo": {
    "id": "1",
    "pageno": "1",
    "source": {
      "id": "G",
      "gurmukhi": "sRI gurU gRMQ swihb jI",
      "unicode": "ਸ੍ਰੀ ਗੁਰੂ ਗ੍ਰੰਥ ਸਾਹਿਬ ਜੀ",
      "english": "Sri Guru Granth Sahib Ji",
      "pageno": "1"
    },
    "raag": {
      "id": "1",
      "gurmukhi": "jp",
      "unicode": "ਜਪ",
      "english": "Jap",
      "startang": null,
      "endang": null,
      "raagwithpage": "Jap (1-8)"
    },
    "writer": {
      "id": "1",
      "gurmukhi": "mÚ 1",
      "unicode": null,
      "english": "Guru Nanak Dev Ji"
    }
  },
  "navigation": {
    "next": "2"
  },
  "gurbani": [{
      "shabad": {
        "id": "1",
        "gurbani": {
          "gurmukhi": "<> siq nwmu krqw purKu inrBau inrvYru Akwl mUriq AjUnI sYBM gur pRswid ]",
          "unicode": "ੴ ਸਤਿ ਨਾਮੁ ਕਰਤਾ ਪੁਰਖੁ ਨਿਰਭਉ ਨਿਰਵੈਰੁ ਅਕਾਲ ਮੂਰਤਿ ਅਜੂਨੀ ਸੈਭੰ ਗੁਰ ਪ੍ਰਸਾਦਿ ॥",
          "searchInputFirstEnglish": "<>snkpnnAmAsgp",
          "searchInputFirstGurmukhi": "ੴਸਨਕਪਨਨਅਮਅਸਗਪ"
        },
        "larivaar": {
          "gurmukhi": "<>siqnwmukrqwpurKuinrBauinrvYruAkwlmUriqAjUnIsYBMgurpRswid]",
          "unicode": "ੴਸਤਿਨਾਮੁਕਰਤਾਪੁਰਖੁਨਿਰਭਉਨਿਰਵੈਰੁਅਕਾਲਮੂਰਤਿਅਜੂਨੀਸੈਭੰਗੁਰਪ੍ਰਸਾਦਿ॥"
        },
        "translation": {
          "english": {
            "ssk": "One Universal Creator God. The Name Is Truth. Creative Being Personified. No Fear. No Hatred. Image Of The Undying, Beyond Birth, Self-Existent. By Guru's Grace ~"
          },
          "punjabi": {
            "bms": {
              "gurmukhi": "Akwl purK ie`k hY, ijs dw nwm 'hoNd vwlw' hY jo isRStI dw rcnhwr hY, jo sB ivc ivAwpk hY, BY qoN rihq hY, vYr-rihq hY, ijs dw srUp kwl qoN pry hY, (Bwv, ijs dw srIr nws-rihq hY), jo jUnW ivc nhIN AwauNdw, ijs dw pRkwS Awpxy Awp qoN hoieAw hY Aqy  jo siqgurU dI ikrpw nwl imldw hY[",
              "unicode": "ਅਕਾਲ ਪੁਰਖ ਇੱਕ ਹੈ, ਜਿਸ ਦਾ ਨਾਮ 'ਹੋਂਦ ਵਾਲਾ' ਹੈ ਜੋ ਸ੍ਰਿਸ਼ਟੀ ਦਾ ਰਚਨਹਾਰ ਹੈ, ਜੋ ਸਭ ਵਿਚ ਵਿਆਪਕ ਹੈ, ਭੈ ਤੋਂ ਰਹਿਤ ਹੈ, ਵੈਰ-ਰਹਿਤ ਹੈ, ਜਿਸ ਦਾ ਸਰੂਪ ਕਾਲ ਤੋਂ ਪਰੇ ਹੈ, (ਭਾਵ, ਜਿਸ ਦਾ ਸਰੀਰ ਨਾਸ-ਰਹਿਤ ਹੈ), ਜੋ ਜੂਨਾਂ ਵਿਚ ਨਹੀਂ ਆਉਂਦਾ, ਜਿਸ ਦਾ ਪ੍ਰਕਾਸ਼ ਆਪਣੇ ਆਪ ਤੋਂ ਹੋਇਆ ਹੈ ਅਤੇ  ਜੋ ਸਤਿਗੁਰੂ ਦੀ ਕਿਰਪਾ ਨਾਲ ਮਿਲਦਾ ਹੈ।"
            }
          },
          "spanish": "Un sólo Creador Universal, Verdad es Su Nombre. Creativa Su Personalidad. Sin Miedo, sin Venganza. Inmortal, Sin Principio, Sin Fin, Auto Existente. Por la Gracia del Verdadero Guru."
        },
        "transliteration": "ikOankaar sat naam karataa purakh nirabhau niravair akaal moorat ajoonee saibha(n) gur prasaadh ||",
        "shabadid": "1",
        "pageno": "1",
        "lineno": "1",
        "updated": "2019-01-31 21:40:51",
        "firstletters": {
          "ascii": ",060,115,110,107,112,110,110,065,109,065,115,103,112,",
          "english": "<>snkpnnAmAsgp"
        },
        "bisram": {
          "sttm": null,
          "igurbani1": null,
          "igurbani2": null
        }
      }
    },
{
      "shabad": {
        "id": "2",
        "gurbani": {
          "gurmukhi": "] jpu ]",
          "unicode": "॥ ਜਪੁ ॥",
          "searchInputFirst": "j"
        },
        "larivaar": {
          "gurmukhi": "]jpu]",
          "unicode": "॥ਜਪੁ॥"
        },
        "translation": {
          "english": {
            "ssk": "Name of the Composition, \"Jup\". Chant And Meditate:"
          },
          "punjabi": {
            "bms": {
              "gurmukhi": "ies swrI bwxI dw nwm 'jpu' hY [",
              "unicode": "ਇਸ ਸਾਰੀ ਬਾਣੀ ਦਾ ਨਾਮ 'ਜਪੁ' ਹੈ ।"
            }
          },
          "spanish": "Canta y medita en esto,"
        },
        "transliteration": "|| jap ||",
        "shabadid": "1",
        "pageno": "1",
        "lineno": "3",
        "updated": "2019-01-31 21:40:51",
        "firstletters": {
          "ascii": ",106,",
          "english": "j"
        },
        "bisram": {
          "sttm": "",
          "igurbani1": "",
          "igurbani2": ""
        }
      }
    },

您可以忽略其中的另一种实际语言,它无关紧要,只是一个字符串。 这只是其中的一小段,它是一个很大的文件。

我已经设法很好地访问每个属性,然后重新分配了新属性,以便可以在JavaScript中引用它们。

for (var iD = 0; iD < data.gurbani.length; iD++) {
    items.push({
      shabadId: data.gurbani[iD].shabad.shabadid,
      id: data.gurbani[iD].shabad.id,
      title: data.gurbani[iD].shabad.gurbani.unicode,
      subtitle: data.gurbani[iD].shabad.translation.english.ssk,
      searchInput: data.gurbani[iD].shabad.firstletters.english
      // data.gurbani[iD].shabad.gurbani.searchInputFirstGurmukhi
    });

我的问题是,我现在需要访问例如“ subtitle”属性(可以是其中的任何一个),以便现在可以读取“ subtitle”下的所有字符串/对象,因此我可以在for循环之外对他们进行其他操作。

仅说明一下,所有功能都可以正常工作,只是能够访问循环外部的属性。内容确实会写到我需要的地方,等等。

我有点新手,所以我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

只需对map执行items即可获得subtitle

const allSubtitles = items.map(e => e.subtitle);