0

I have an array of

[{
    "39195": {
        "name": "Introduction",
        "lessons": [{
            "name": "A",
            "duration": "(05:30)",
            "movieName": "Why+Learn+ActionScript%3F"
        }, {
            "name": "About the Included Sample Scripts",
            "duration": "(03:49)"
        }, ]
    },
    "39196": {
        "name": "Introduction2",
        "lessons": [{
            "name": "B",
            "duration": "(05:30)",
            "movieName": "Why+Learn+ActionScript%3F"
        }, {
            "name": "About the Included Sample Scripts",
            "duration": "(03:49)"
        }, ]
    },
    "39197": {
        "name": "Introduction3",
        "lessons": [{
            "name": "C",
            "duration": "(05:30)",
            "movieName": "Why+Learn+ActionScript%3F"
        }, {
            "name": "About the Included Sample Scripts",
            "duration": "(03:49)"
        }, ]
    }
}]

How can i get array of values of [Introduction,Introduction2,Introduction3]

7
  • are you sure this is valid ? Commented Apr 28, 2016 at 7:53
  • 1
    Would love to see what you've tried. Commented Apr 28, 2016 at 7:53
  • what i mean is the sample above. is it valid? Commented Apr 28, 2016 at 7:54
  • 1
    @guradio, no it isn't a few commas extra/missing in some places.Just ran it through JSONLINT. Commented Apr 28, 2016 at 7:55
  • Yes, this is the response i got from the above api Commented Apr 28, 2016 at 7:55

2 Answers 2

1

You can use Object.keys() and map()

var arr = [{
  "39195": {
    "name": "Introduction",
    "lessons": [{
      "name": "A",
      "duration": "(05:30)",
      "movieName": "Why+Learn+ActionScript%3F"
    }, {
      "name": "About the Included Sample Scripts",
      "duration": "(03:49)"
    }]
  },
  "39196": {
    "name": "Introduction2",
    "lessons": [{
      "name": "B",
      "duration": "(05:30)",
      "movieName": "Why+Learn+ActionScript%3F"
    }, {
      "name": "About the Included Sample Scripts",
      "duration": "(03:49)"
    }]
  },
  "39197": {
    "name": "Introduction3",
    "lessons": [{
      "name": "C",
      "duration": "(05:30)",
      "movieName": "Why+Learn+ActionScript%3F"
    }, {
      "name": "About the Included Sample Scripts",
      "duration": "(03:49)"
    }]
  }
}];

var res = Object.keys(arr[0]) // get get object keys for iteration
  .map(function(v) { // iterate over the array and retrieve needed property
    return arr[0][v].name // get name property from inner object
  });

document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');

Sign up to request clarification or add additional context in comments.

Comments

0

This would do the trick. Set a = to json data

var names = [];
for (var key in a[0]) {
  if (a[0].hasOwnProperty(key)) {
    alert("prop: " + key + " value: " + a[0][key]['name']);
    names.push(a[0][key]['name']);
  }
}

If it really is an array then you could add another loop to loop over the array.

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.