This looks a simple code but not sure what I am doing wrong here.
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if(item.Id != "01" || item.Id !="02")
{
alert(item.Id);
}
}
I am expecting to just show an alert as "03" but it shows 3 alert each for "01", "02" & "03"
Looks like != and OR operator not working ?
This looks a simple code but not sure what I am doing wrong here.
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if(item.Id != "01" || item.Id !="02")
{
alert(item.Id);
}
}
I am expecting to just show an alert as "03" but it shows 3 alert each for "01", "02" & "03"
Looks like != and OR operator not working ?
Share Improve this question asked May 30, 2018 at 13:32 amanaman 6,27217 gold badges64 silver badges118 bronze badges 2- 3 You want an AND not an OR. – dimwittedanimal Commented May 30, 2018 at 13:35
-
1
Think about it logically and step through your code. when
item.Id
is "01", the first half of the OR is false, but the second half of the OR is true, so it evaluates to true. Whenitem.Id
is "02", the halves switch, but the oute is still true. – Heretic Monkey Commented May 30, 2018 at 13:37
3 Answers
Reset to default 8The ||
operator only returns false
when both of its operands are false
(and true
in all other cases).
The &&
operator only returns true
when both of its operands are true
(and false
in all other cases).
You have to use &&
instead of ||
in the if condition to get the expected result:
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if(item.Id != "01" && item.Id !="02")
{
alert(item.Id);
}
}
Actually is working as expected because the OR
operator is returning true
for cases when Id == 01
and Id == 02
What you want to do is to join the two operands with AND operator &&
and this way execute the logic just when Id != 01
AND Id != 02
.
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if (item.Id != "01" && item.Id != "02") {
alert(item.Id);
}
}
item.Id != "01" || item.Id !="02"
will always be true, as it can't be equal to both of the values at the same time.
You only need to use &&
instead of ||
:
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if(item.Id != "01" && item.Id !="02")
{
console.log(item.Id); // I prefer consoling, that's less agressive!
}
}
⋅ ⋅ ⋅
If you also plan to do if(item.Id != "01" && item.Id !="02" && item.Id !="03")
, you must want to use a >
parator and unary +
on your string:
var data = "[{\"Id\":\"01\",\"Name\":\"01- Alabama\"},{\"Id\":\"02\",\"Name\":\"02- Arizona\"},{\"Id\":\"03\",\"Name\":\"03- Arkansas\"}]";
var myJson = JSON.parse(data);
for (var key in myJson) {
var item = {
Id: myJson[key].Id,
Name: myJson[key].Name
};
if(+item.Id > 2)
{
console.log(item.Id); // I prefer consoling, that's less agressive!
}
}
Hope it helps.