1

I have some JSON data that I'm trying to extract.

I'm trying to extract and reformat some of the data to an array. I'm looping through the data, but am having problems extracting the data within a nested submeaning object.

JSON data:

var data = [
  {
    "meaning": "a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use.",
    "examples": [
      "no sparking was visible during the tests"
    ],
    "submeanings": [
      {
        "meaning": "a short written or spoken examination of a person's proficiency or knowledge.",
        "examples": [
          "a spelling test"
        ]
      },
      {
        "meaning": "an event or situation that reveals the strength or quality of someone or something by putting them under strain.",
        "examples": [
          "this is the first serious test of the peace agreement"
        ]
      },
      {
        "meaning": "an examination of part of the body or a body fluid for medical purposes, especially by means of a chemical or mechanical procedure rather than simple inspection.",
        "examples": [
          "a test for HIV",
          "eye tests"
        ]
      },
      {
        "meaning": "a procedure employed to identify a substance or to reveal the presence or absence of a constituent within a substance."
      }
    ]
  },
  {
    "meaning": "a movable hearth in a reverberating furnace, used for separating gold or silver from lead."
  }
]

Algorithm:

// array to hold definitions
var definitions = [];

for (var i = 0; i < data.length; i++) {
    // push first 
    definitions.push(data[i]['meaning']);

    // push second, if submeaning data exists
    if (data[i]['submeanings'].length >= 1) {
        definitions.push(data[i]['submeanings'][i]['meaning']);
    }
}

When I run this code, I'm getting the following error:

Uncaught TypeError: Cannot read property 'length' of undefined(…)

Any help is appreciated.

3 Answers 3

1

Check if submeanings exists, before you ask for it's length.

// push second, if submeaning data exists
if (data[i] && data[i]['submeanings'] && data[i]['submeanings'].length >= 1) {
    definitions.push(data[i]['submeanings'][i]['meaning']);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to check if the objects have submeanings before checking their length. Change

if (data[i]['submeanings'].length >= 1)

to

if (data[i]['submeanings'] && data[i]['submeanings'].length >= 1)

Furthermore, if there are multiple submeanings, you'll need a separate loop to extract the submeanings, like so:

if (data[i]['submeanings'] && data[i]['submeanings'].length >= 1) {
    for(var j = 0; j < data[i]['submeanings'].length; j++) {
        definitions.push(data[i]['submeanings'][j]['meaning']);
    }
}

Keeping track of multiple indices is difficult though, so I'd suggest looking into using forEach instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Comments

0

That because you don't have attribute submeanings in the second object of your JSON data, so you should check if the object has property using hasOwnProperty() then get it :

if (data[i].hasOwnProperty('submeanings')) {
    definitions.push(data[i]['submeanings'][i]['meaning']);
}

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.