-1

I've got a json array like this:

var weekdayarrayA = [{
        "course": {
            "id": 44,
            "name": "test".......
        },
        "data": {
            "0": 2,
            "1": 1,
            "4": 1
        }
    }, {
        "course": {
            "id": 45,
            ,
            "name": "test2".......
        },
        "data": {
            "0": 2,
            "1": 1,
            "4": 1
        }
    },

What I need is to get from each course the name and the data array

How can I access thos data?

Thanks a lot

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
Felix
  • 5,452
  • 12
  • 68
  • 163

2 Answers2

2

here is:

var weekdayarrayA = [
{"course": {"id":44, "name": "test1"}, "data": {"0":2,"1":1,"4":1}},
{"course": {"id":45, "name": "test2"}, "data": {"0":2,"1":1,"4":1}}
];

for(var i in weekdayarrayA) {
 var item = weekdayarrayA[i];
 var course = item.course;
 var courseData = item.data;
 alert(course.name);
}
num8er
  • 18,604
  • 3
  • 43
  • 57
  • 4
    `for...in` loops should be avoided with arrays. This will break if someone adds a new property to `Array.prototype`. Use a regular for loop instead, or the `forEach()` function available in modern browsers. ES6 introduces `for...of` loops so that will be an option soon too (and already works in node.js). – Matt Browne Jul 18 '15 at 15:29
  • @matt-browne, I appreciate Your comment. But I insist on that: for...in works well with all browser. What if it's not an regular array with indexes like: 0,1,2 ? of course forEach is better solution, and for...of is the best. but have to wait while all devices get support of it... – num8er Jul 18 '15 at 15:36
  • I think you may be missing the point... if I do `Array.prototype.foo = 'foo'` prior to running your example, it will actually give a TypeError because item.course.name is undefined for 'foo'. – Matt Browne Jul 18 '15 at 15:38
  • And you have to account for the possibility that some other JS code in your app (including 3rd party code) might have modified `Array.prototype`. – Matt Browne Jul 18 '15 at 15:39
  • I understood, but why to make so? who changes the prototype of array so? :) we can add functions, attributes to prototype but it will not change Array to be what it is. – num8er Jul 18 '15 at 15:40
  • Actually if it were a function there would still be the same issue, e.g. `Array.prototype.foo = function() {};`. The only way it wouldn't cause an issue is if the property were declared as non-enumerable. I am not alone in this opinion; if you do a Google search you'll see there's widespread agreement that `for..in` loops should not be used for arrays. And of course it's not only the prototype that matters...if I did `weekendarrayA.foo = ...` that would also cause an issue. – Matt Browne Jul 18 '15 at 15:45
  • I appreciate You for explanation. But kindness is what we have (no need to send me to google it). – num8er Jul 18 '15 at 15:52
1

You can iterate over the array and retrieve values like following.

for (var i = 0 ; i < weekdayarrayA.length; i++) {
     console.log(weekdayarrayA[i].course.name);
     console.log(weekdayarrayA[i].data);
}

Note: Data is not an array, rather it is an object.

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59