开发者

jQuery if object has value

开发者 https://www.devze.com 2023-04-06 02:27 出处:网络
I have an object built up like so: tabArray.push({\'linkId\': \'tabBuild\', \'id\': \'columnRight\',\'url\': \'/pages/publish/\', \'content\':response});

I have an object built up like so:

tabArray.push({'linkId': 'tabBuild', 'id': 'columnRight','url': '/pages/publish/', 'content':response});

How can i use jQuery/javascript to do a test on that object to see if it contains a certain URL?

I tried this: if($.inArray('/pages/publish/', tabArray) > -1) aler开发者_StackOverflowt('its in there');

But to no avail!


Try the following

var inArray = false;
for (var i = 0; i < tabArray.length; i++) {
  if (tabArray[i].url === '/pages/publish') {
    inArray = true;
    break;
  }
}


This should work:

var inArray = !!$.grep(tabArray, function(item) {
        return item.url === '/pages/publish/';
    }).length;

console.log(inArray); // true


$.inArray won't work as it is only meant to search an array of strings while your array is an array of objects. You can create your own in array method.

var inURLArray = function(url, array) {
  for(var i in array) {
    if(array[i].url && array[i].url === url)
    return true;
  }
  return false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消