0

Im unable to figure out how to check if my List Contains a element.

 for (x in chatBoxes) {
            chatboxtitle = chatBoxes[x];

            if (chatboxtitle==obj[ i ].from) {
            //alert(obj[ i ].from + " YES !");
            } else {
            //alert(obj[ i ].from + " NOPE !");
            }
            };

Question How does it work ? Or how to code it like If chatboxes contains obj[ i ].from then yes else no Cause at this moment it only works when there is a element in the Chatboxes List... If there is nothing then happens nothing

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Varex Dev's
  • 51
  • 1
  • 9

2 Answers2

0

Assuming chatBoxes is an array, use indexOf()

if (chatBoxes.indexOf(obj[ i ].from) > -1)
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
0

If chatBoxes is an object, you need to use the flag to findout whether the object contains the said value, then after the loop you need to do the action which is depending on the contains flag.

var contains = false;
for (x in chatBoxes) {
    if (chatBoxes[x] == obj[i].from) {
        contains = true;
        break;
    }
};

if (contains) {
    alert(obj[i].from + " YES !");
} else {
    alert(obj[i].from + " NOPE !");
}

But if chatBoxes is an array

if(chatBoxes.indexOf(obj[i].from) > -1){
    alert(obj[i].from + " YES !");
} else {
    alert(obj[i].from + " NOPE !");
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531