我有一个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或包含我可以使用的功能?
答案 0 :(得分: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不匹配的项目。
答案 1 :(得分: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);
}
答案 2 :(得分: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)
答案 3 :(得分: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
答案 4 :(得分: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))