2

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?

3 Answers 3

1

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

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

Comments

1

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))

Comments

0

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'),
    });
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.