0

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

2 Answers 2

1

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;   
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer, then what should i use to show the new value in my list ? curently i use {{device.status}} (from device in devices)
This answer will override the status in json to string(On/Off), you can still use it as device.status. Do you wish to create a new property keeping the original status as (1/0)?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.