javascript - Check for boolean values and convert them to numbers in an object -


I've found an object whose values ​​are true and false some keys for. I have to find out which key has got boolean values ​​and they can be converted to 1 and 0, respectively, how do I do this? Any help or advice is highly appreciated. 123, "accountid": 456, "name": "Test in progress", "number": [{' SetNumber ': true}, {' setNumber ': false},], "settings": {"playback": wrong, "attached file": true,}}

You can number or + operator . Change a Boolean (number) using

 ; // = & gt; 1 number (wrong); // = & gt; 0 + (true); // = & gt; 1 + (wrong); // = & gt; 0  

[ Edit ) based on the comment You can use typeof to evaluate the value for a boolean Are:

somebool = true, sombool_isBoolean = /^boolean$/i.test (types of somebool) // = & gt; Truth, nobool = 'true', nobool_isBoolean = / ^ Boolean $ / i examination (nobool of type) // = & gt; wrong;

So you can do something like this:

  var objnr0 = obj.number [0] .setNumber; Obj.number [0] .setNumber = /^boolean$/i.test(objnr0)? + (Objnr0): objnr0;  

[ edit2 based on comments]

To change the actual / false boolean values ​​of an object, a dirty object of 'dirty tricks' To do the looping:

var obj2 = JSON.parse (JSON.stringify (obj) .replace (/: true / gi, ': 1') .replace (/: false / gi, ': 0'));

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -