如何在Angular中更改从json url检索的值

时间:2016-06-02 16:12:01

标签: angularjs json parsing

我有一个从网址检索JSON的服务,我使用JSON在列表中显示值。

我的[ {"iconuser":"livingroom1","class":"w5","status":"0"}, {"iconuser":"meetingroom1","class":"w4","status":"1"} ] 看起来像这样:

status = 0

如何替换该对象的某些值。

示例:

status = OFF应为status = 1
status = ON应为(output * sd) + mean

2 个答案:

答案 0 :(得分:1)

您可以使用Array.map格式化您的回复:

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

    return obj;
});

答案 1 :(得分:1)

原生角度:

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