152

I have a model that is set with a JSON response from a mysql database. The model data is set with true or false into a boolean/tinyint field in the database, which uses 1 or 0.

In my view, I have a binding that checks for a boolean with underscore's _.isBoolean. Of course, when my model receives the data, it is set with 1 or 0 instead of true or false and the _.isBoolean check fails.

Is there anyway to either correctly have my JSON response from mysql be a boolean true or false value instead of 1 or 0, or preferably, is there a way to have my model update itself upon fetch (and before the view renders) to cast true or false based on it's 1 or 0 property?

e.g. my model's data looks like {"isChecked":"1"} when I need it to be {"isChecked":true}

Thank you greatly for any suggestions you may have!

3
  • what is the server side technology used? Commented May 1, 2013 at 5:30
  • PHP, the response is being written out with a json_encode on the query result
    – Chris M
    Commented May 1, 2013 at 5:34
  • did you try with the way: isChecked=isChecked?true : false
    – Ulug'bek
    Commented May 1, 2013 at 6:00

6 Answers 6

240

All you need is convert string to int with + and convert the result to boolean with !!:

var response = {"isChecked":"1"};
response.isChecked = !!+response.isChecked

You can do this manipulation in the parse method:

parse: function (response) {
  response.isChecked = !!+response.isChecked;
  return response;
}

UPDATE: 7 years later, I find Number(string) conversion more elegant. Also mutating an object is not the best idea. That being said:

parse: function (response) {
  return Object.assign({}, response, {
    isChecked: !!Number(response.isChecked), // OR
    isChecked: Boolean(Number(response.isChecked))
  });
}
1
  • 19
    +1 For converting into int first. !!+"1"; // true, !!+"0"; // false, !!+1; // true, !!+0; // false Commented May 1, 2013 at 6:18
104

Use a double not:

!!1 = true;

!!0 = false;

obj.isChecked = !!parseInt(obj.isChecked);
3
  • 2
    This worked for me but for some reason I had an I still had to parse it out value = (!!parseInt(value) ? true : false)
    – Rick
    Commented Apr 13, 2015 at 5:09
  • Just to be sure, !!2 or any other number = false, right? Commented Mar 18, 2020 at 20:46
  • No, !!2 would be true, it'll only be false for 0
    – dave
    Commented Mar 18, 2020 at 22:30
98

Here's another option that's longer but may be more readable:

Boolean(Number("0")); // false
Boolean(Number("1")); // true
0
20

Assigning Comparison to property value

JavaScript

You could assign the comparison of the property to "1"

obj["isChecked"] = (obj["isChecked"]==="1");

This only evaluates for a String value of "1" though. Other variables evaulate to false like an actual typeof number would be false. (i.e. obj["isChecked"]=1)

If you wanted to be indiscrimate about "1" or 1, you could use:

obj["isChecked"] = (obj["isChecked"]=="1");

Example Outputs

console.log(obj["isChecked"]==="1"); // true
console.log(obj["isChecked"]===1); // false
console.log(obj["isChecked"]==1); // true
console.log(obj["isChecked"]==="0"); // false
console.log(obj["isChecked"]==="Elephant"); // false

PHP

Same concept in PHP

$obj["isChecked"] = ($obj["isChecked"] == "1");

The same operator limitations as stated above for JavaScript apply.

Double Not

The 'double not' also works. It's confusing when people first read it but it works in both languages for integer/number type values. It however does not work in JavaScript for string type values as they always evaluate to true:

JavaScript

!!"1"; //true
!!"0"; //true
!!1; //true
!!0; //false
!!parseInt("0",10); // false

PHP

echo !!"1"; //true
echo !!"0"; //false
echo !!1; //true
echo !!0; //false
1
  • good idea Christopher, i was able to achieve this by writing: ` this.model.set('isChecked', (this.model.get('isChecked') === '1'))` in the initialize function of my backbone view.
    – Chris M
    Commented May 1, 2013 at 5:49
2
Boolean(Number(myVar))

or

!!+myVar
1
  • This is duplicating previous answers. IMO it should be deleted to minimize the clutter.
    – Ian Dunn
    Commented Dec 29, 2022 at 16:54
1

Another work around is to simply create an array holding the booleans and assigning the corresponding boolean to it's index. Let me explain below:

const bools = [false,true];

Access the boolean you want with either int or str.

bools['0'] (str) or bools[0] (int) should work just fine in the browser and yield the correct value. I recently used this for setting a checkbox value.

Hope this helped

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.