English 中文(简体)
如何最快地检查在使用Jquery或Javascript的阵列中是否存在具体的用户信息。
原标题:What is the fastest way to check whether a specific UserID exists in the array using Jquery or Javascript

我有一系列物体<代码>g AllMedicalFilesClaimants Array,有2种特性(UserID &userInfo)

例如:

gAllMedicalFilesClaimantsArray[0].UserID = "111"; 
gAllMedicalFilesClaimantsArray[0].UserInfo = "AAA-111";
gAllMedicalFilesClaimantsArray[1].UserID = "222"; 
gAllMedicalFilesClaimantsArray[1].UserInfo = "BDD-478333";

如何最快地检查在使用Jquery或Javascript的阵列中是否存在具体的用户信息,因为g AllMedicalFilesClaimants Array有8000份记录?

增 编

问题回答
var match =  222 ;
var matches = $.grep(myArray, function(el, index) {
   return (el.UserID === match);
});

如果对阵列进行分类(例如与用户Id有关),你可以加快搜索过程。

function binarySearch(array, userid) {
  var low = 0, high = array.length - 1,
      i, comparison;
  while (low <= high) {
    i = parseInt((low + high) / 2, 10);

    if (array[i].UserId < userid) { low = i + 1; continue; };
    if (array[i].UserId > userid) { high = i - 1; continue; };
    return array[i];
  }
  return null;
};

可以通过使用以下功能找到其12个用户:

var result = binarySearch(gAllMedicalFilesClaimantsArray, 12);

与此类似,我认为:

function exists(uid) {
    var k = gAllMedicalFilesClaimantsArray.length;
    uid = uid.toString(); // ensure the arg is a str (this can be omitted)
    while (k--) {
        if (gAllMedicalFilesClaimantsArray[k].UserID === uid) {
            return true;
        }
    }
    return false;
}

用户信息数据库是否对阵列进行了分类? 如果是的话,可以通过使用双轨搜索加以进一步改进;这将从O(n)改为O(log n)。 你们的例子表明了这一点。 我发现,Java在网上的双轨搜索得到了很好的执行,网址是:here。 如果该网址已经死亡,该守则就是:

function binarySearch(items, value){

    var startIndex  = 0,
        stopIndex   = items.length - 1,
        middle      = Math.floor((stopIndex + startIndex)/2);

        while(items[middle] != value && startIndex < stopIndex){

        //adjust search area
        if (value < items[middle]){
            stopIndex = middle - 1;
        } else if (value > items[middle]){
            startIndex = middle + 1;
        }

        //recalculate middle
        middle = Math.floor((stopIndex + startIndex)/2);
    }

    //make sure it s the right value
    return (items[middle] != value) ? -1 : middle;
}
ExistsInArray(value, array){
    for(var item in array){
        if(item.UserId == value){
            return true;
        }
    }
    return false;
}

您可以像现在一样,模版Array物体:

Array.prototype.exists = function(value, prop){
    var i = null;
    for (i in this)
        if (this[i][prop] && this[i][prop] == value)
            return true;
    return false;
}

gAllMedicalFilesClaimantsArray.exists( 222 ,  UserID );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签