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]

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Hulk1991
  • 3,079
  • 13
  • 31
  • 46

2 Answers2

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>');
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
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.

Bill King
  • 72
  • 9