Go through JSON Object

7

I have this json:

var json =    {
     "0":{
       "check":true,
       "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
         },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }}
     "1":{
      "check":true,
      "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
      },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }
     }
    }

I would like if I get 0 or 1, change the value of ObJECT_ID and EVENT_NAME to true

I've tried doing this for:

    for (var i in json) {
        console.log(json[i].nameTable)
    }

As I get 0 or 1, I have to go through what's inside them (in my example I only have 2 the ObJECT_ID and EVENT_NAME but I have many more) and change the check value inside them by true

    
asked by sirdaiz 27.04.2017 в 11:41
source

2 answers

8

Check the following program. It verifies the existence of the property check of each element to assign it true if so.

let json = {
      "0":{
       "check":true,
       "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
         },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }},
     "1":{
      "check":true,
      "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
      },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }
     }
    };
 
for (let i in json) {
  for (let j in json[i]) {
    if (json[i][j].hasOwnProperty('check')) {
      json[i][j].check = true;
    }
  }
}

console.log("Mostrando resultado final:");
console.log(json);

Depending on the edition you provide, it is more efficient to do the following:

let json = {
      "0":{
       "check":true,
       "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
         },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }},
     "1":{
      "check":true,
      "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
      },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }
     }
    };
 
/* Sólo debemos cambiar los elementos 0 y 1 si existen */
$scope.toggle = function(isCheck, index) {
 if (json.hasOwnProperty(index)) {
  /* No es necesario usar index.toString() */
   for (let j in json[index]) {
    if (json[index][j].hasOwnProperty('check')) {
      json[index][j].check = isCheck;
    }
   }
 }
}

console.log("Mostrando resultado final:");
console.log(json);

If you receive, as you say in your question, 50 or more records it is a waste of time to go through each and every one of the elements if you only look for two in particular, the index 0 and the 1 .

    
answered by 27.04.2017 / 12:10
source
2

Let's see if I understood you correctly.

Do you want to put true the check properties of the item that you are told?

If so, here is an example where a function receives the id of the element to modify and the object json and modifies its properties check .

var json =    {
     "0":{
       "check":true,
       "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
         },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }},
     "1":{
      "check":true,
      "OBJECT_ID":{
         "check":false,
         "name":"OBJECT_ID",
      },
      "nameTable":"TEST1",
      "EVENT_NAME_MANAGE":{
         "check":false,
         "name":"EVENT_NAME_MANAGE",
      }
     }
    }
    
function check(id, jsonObject){
  if (jsonObject[id] && jsonObject[id].OBJECT_ID){
    jsonObject[id].OBJECT_ID.check = true;
  }
  if (jsonObject[id] && jsonObject[id].EVENT_NAME_MANAGE){
    jsonObject[id].EVENT_NAME_MANAGE.check = true;
  }  
}

check('1', json);

console.log(json);
    
answered by 27.04.2017 в 12:20