condtion为true时for循环重复

时间:2017-03-26 09:02:17

标签: javascript

我有json值。

这是我的JSON。

"notifications" : {
    "0" : {
      "FROM" : 1,
      "MSG" : "Admin Saravanan has sent new circular (Message from admin) ",
      "RECEIVE_TIME" : "2017-03-18 06:17:26",
      "SEND_TIME" : "2017-03-17 02:38:01",
      "STATUS" : "ON",
      "TO" : "ALL",
      "TYPE" : "EVENT"
    },
    "1" : {
      "FROM" : 1,
      "MSG" : "New visitor muruganantham.k.m checkin your flat.",
      "RECEIVE_TIME" : "2017-03-23 06:33:06",
      "SEND_TIME" : "2017-03-23 11:45:26 am",
      "STATUS" : "ON",
      "TO" : "2",
      "TYPE" : "VISITORS"
    }

并将此json分配给arr值。

这是我的功能

for(var i=0;i<arr.length; i++)
        {
            if(arr[i].STATUS === 'ON')
            {
                this.play();
            }   
        }

        this.play()
        {
            //arr[0].STATUS changed to OFF
            //Here Updated the value 
            //the status value changed to OFF.
        }

我需要for循环首先arr [0]值改变状态OFF.after将alr [1]值。

我该怎么办?

Kinldy建议我,

感谢。

1 个答案:

答案 0 :(得分:0)

您无法将JSON视为数组。 如果我明白你想要做什么(我不完全确定),它应该是这样的: 使用Object.keys(通知)来获取JSON对象的键,而不是切换状态。

var notifications = {
    "0" : {
        "FROM" : 1,
        "MSG" : "Admin Saravanan has sent new circular (Message from admin) ",
        "RECEIVE_TIME" : "2017-03-18 06:17:26",
        "SEND_TIME" : "2017-03-17 02:38:01",
        "STATUS" : "ON",
        "TO" : "ALL",
        "TYPE" : "EVENT"
    },
    "1" : {
        "FROM" : 1,
        "MSG" : "New visitor muruganantham.k.m checkin your flat.",
        "RECEIVE_TIME" : "2017-03-23 06:33:06",
        "SEND_TIME" : "2017-03-23 11:45:26 am",
        "STATUS" : "ON",
        "TO" : "2",
        "TYPE" : "VISITORS"
    }
}

var keys = Object.keys(notifications);
if (notifications.hasOwnProperty(keys[0]) {
    if (notifications[keys[0]].STATUS == 'ON') {
        notifications[keys[0]].STATUS = 'OFF';
    }
 }