diego jimenez
diego jimenez

Reputation: 83

How to change values retrieved from json url in Angular

I have a service that retrieves a JSON from an url, I use ng-repeat to show values in a list.

My JSON looks like this:

[
  {"iconuser":"livingroom1","class":"w5","status":"0"},     
  {"iconuser":"meetingroom1","class":"w4","status":"1"}
]

How do I replace some values of that object.

example:

status = 0 should be status = OFF
status = 1 should be status = ON

Upvotes: 0

Views: 110

Answers (2)

Sajal
Sajal

Reputation: 4401

In native angular:

$scope.item = [{"iconuser":"livingroom1","class":"w5","status":"0"}, 
{"iconuser":"meetingroom1","class":"w4","status":"1"}];

angular.forEach($scope.item,, function(obj) {
    if(obj.status === 0)
      obj.status = "OFF";
    else
      obj.status = "ON";

    return obj;   
});

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

You can use Array.map to format your response:

var formattedData = responseData.map(function(obj) {
    if (obj.status === 0) {
        obj.status = "OFF";
    } //etc

    return obj;
});

Upvotes: 1

Related Questions