What is wrong with this conditional?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • X Enterprises
    New Member
    • Dec 2007
    • 15

    #1

    What is wrong with this conditional?

    I'm working on a Chrome extension an I've hit a wall.

    Code:
    function isInQueue(id) {
    	chrome.extension.sendRequest({getQueueItems: 1}, function(response) {
    		var items = response.items;
    		if (items) {
    			for (var i = 0; i < items.length; i++) {
    				if ((items[i].id == id) == true) return true;
    			}
    			return false;
    		} else { return false; }
    	});
    }
    The request returns 'items' which is an array of objects. I am trying to see if another item outside of the queue already exists inside the queue. For example, there is an item on the outside with an id equal to '198677'. I know I already have an exact copy of that same item in my queue with the exact same id, '198677', however, when I test the two for equality
    Code:
    (items[i].id == id) == true
    , it returns false. I have checked the typeof both and they are both strings. I have tried using === and that hasn't worked. I tried adding zero to each of them to turn them into integers and that made the function return true when it was actually true, however, when I tested for true
    Code:
    if (isInQueue(id) == true)
    the conditional returned false.

    This is all very confusing and frustrating for me. They're both strings, why doesn't it work?

    Help is very much appreciated,
    X Enterprises.
  • X Enterprises
    New Member
    • Dec 2007
    • 15

    #2
    I figured it out. I actually have the queue stored in localStorage as stringified JSON.
    Code:
    function isInQueue(id, callback) {
        var result = false;
        var queue = localStorage["queue_rss"];
        if (queue != undefined) {
            var items = JSON.parse(queue).items;
            if (items) {
                for (var i = 0; i < items.length; i++) {
                    if ((items[i].id == id) == true) {
                      result = true;
                      break;
                    }
                }
            }
        }
        callback(result);
    }

    Comment

    Working...