Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to map type and url value inside media2 from this json array to angularjs scope Array

"results":[
    {
        "session2":[
            {
                "__type":"Object",
                "about":"about here",
                "attendant2":{
                    "__type":"Relation",
                    "className":"_User"
                },
                "className":"sessionClass",
                "createdAt":"2015-09-16T06:12:54.461Z",
                "media2":[
                    {
                        "__type":"Object",
                        "priority":1,
                        "type":3,
                        "updatedAt":"2015-09-16T06:12:54.153Z",
                        "url":"https://example.com"
                    }
                ],
                "updatedAt":"2015-09-16T06:12:54.461Z"
            }
        ],
        "updatedAt":"2015-10-16T06:08:57.726Z"
    }
]

I've tried using this

$scope.mediaType = results['object'].map(function(item) {
                return item.get('session2')[0].get('media2')[0].get('type');
});

and this

$scope.mediaType = results['session2'].map(function(item) {
            return item.get('media2')[0].get('type');
});

but it always give me TypeError: results.object is undefined or TypeError: results.session2 is undefined
How can i get the value from that json?

share|improve this question

results is an array and not object, you can access it by using results[0].session2.map

share|improve this answer

try this

var x = {
  "results": [{
    "session2": [{
      "__type": "Object",
      "about": "about here",
      "attendant2": {
        "__type": "Relation",
        "className": "_User"
      },
      "className": "sessionClass",
      "createdAt": "2015-09-16T06:12:54.461Z",
      "media2": [{
        "__type": "Object",
        "priority": 1,
        "type": 3,
        "updatedAt": "2015-09-16T06:12:54.153Z",
        "url": "https://www.youtube .com/watch?v=cVjT1QGilAg"
      }],
      "updatedAt": "2015-09-16T06:12:54.461Z"
    }],
    "updatedAt": "2015-10-16T06:08:57.726Z"
  }]
}


var y = x.results.map(function(v) {
  return {
    type: v.session2[0].__type,
    url: v.session2[0].media2[0].url
  }
})
document.write(JSON.stringify(y))

share|improve this answer

I am using this way from an answer in another question

$scope.mediaType = [];
var media2 = results.get('session2')[0].get('media2');

for (i = 0; i < media2.length; i++) {
    $scope.mediaType.push({
        type: session[i].get('type'),
        url: session[i].get('url'),
    });
};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.