2

我有一个 Json 对象数组

   posturlContent = [
        { "Title": "Bbc news live", "Content": "<div>this is a test BBC </div>", "Uri": "http://bbc.co.uk" },
        { "Title": "CNN news live", "Content": "<div>this is a test CNN</div>", "Uri": "http://cnn.com" },
        { "Title": "Youtube news live", "Content": "<div>this is a test Youtube </div>", "Uri": "http://youtube.com" },
    ];

我有一个 JS 数组

uris =["http://cnn.com", "http://abcnews.com",...]

我需要检查 posturlContent Json obejct 中是否存在任何 uris 数组元素,并找到不存在的元素。我知道的唯一方法是做嵌套循环。

我可以使用更简单的 .inArray 或 Contains 函数吗?

4

5 回答 5

1

你可以使用$.grep

$.grep(posturlContent, function(item){return item.Uri == siteUrl}).length > 0

如果这是真的,则该项目在数组内。

然后将其包装在$.each

$.each(uris, function(){
    var siteUrl = this.valueOf();
    // Grep and do whatever you need to do.
})

您可以像这样删除项目:

$.each(uris, function(){
    var siteUrl = this.valueOf();
    posturlContent = $.grep(posturlContent, function(item){
        return item.Uri.toLowerCase() != siteUrl.toLowerCase();
    });
})

请注意,我们现在正在过滤与 siteUrl 不匹配的项目。

于 2013-04-16T12:13:34.317 回答
1

如果创建一个 url 缓存对象呢?这是一个小提琴http://jsfiddle.net/U94xT/1/

var posturlContent = [
    { "Title": "Bbc news live", "Content": "<div>this is a test BBC </div>", "Uri": "http://bbc.co.uk" },
    { "Title": "CNN news live", "Content": "<div>this is a test CNN</div>", "Uri": "http://cnn.com" },
    { "Title": "Youtube news live", "Content": "<div>this is a test Youtube </div>", "Uri": "http://youtube.com" },
];
var uris =["http://cnn.com", "http://abcnews.com"];

var urisCache = {}; // using object to avoid urls duplicates, that could occur using an array
for (var i = 0; i < posturlContent.length; i++) {
    var key = posturlContent[i]["Uri"];
    urisCache[key] = true; // or urisCache[key] = posturlContent[i]; if you need to access the corresponding entry from posturlContent
}

//now check if the uris array element exist in posturlContent
for (var i = 0; i < uris.length; i++) {
     alert(uris[i] in urisCache);
}
于 2013-04-16T12:31:57.470 回答
0

使用JQuery grep 可以找到exists,通过取差可以找到nonExists;

Array.prototype.diff = function(a) {
  return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};

var exists = [];
jQuery.grep(posturlContent, function(n, i){
  var index = uris.indexOf(n.Uri);
  if(index >=0){
       exists.push(uris[index])
  }
});

var nonExists = uris.diff(exists)
于 2013-04-16T12:24:56.220 回答
0

您可以使用它grep()来查找存在和不存在的 uri。我不确定它与编写自己的循环相比有多快,但它肯定更简单

var posturlContent = [
        { "Title": "Bbc news live", "Content": "<div>this is a test BBC </div>", "Uri": "http://bbc.co.uk" },
        { "Title": "CNN news live", "Content": "<div>this is a test CNN</div>", "Uri": "http://cnn.com" },
        { "Title": "Youtube news live", "Content": "<div>this is a test Youtube </div>", "Uri": "http://youtube.com" },
    ];

var uris =["http://cnn.com", "http://abcnews.com"];

//keep track of URI's that exist and that are missing
var found = [];
var missing = [];

//for is faster than each()
for(var i=0; i<uris.length; i++) {
    found.concat($.grep(posturlContent, function(item){return item.Uri === uris[i]}, false));
    missing.concat($.grep(posturlContent, function(item){return item.Uri === uris[i]}, true));
}

//do something with found and missing urls
于 2013-04-16T12:26:57.353 回答
0

这是一个没有 jQuery 和没有 forEach 的答案,因为您需要旧的浏览器支持......

for(var i =0; i<posturlContent.length; i++) {
  var detail = posturlContent[i];
  for(var j =0; j<uris.length; j++) {
    var uri = uris[j];
    if (detail.Uri === uri) {
      console.log('matched ' + detail.Uri);
    } else {
      console.log('didnt match ' + detail.Uri);
    }
  }
}

如果您想忽略大小写,那么您可以使用 === 而不是:-

if (new RegExp(uri, "i").test(details.Uri))
于 2013-04-16T12:39:38.827 回答