2011-03-15 50 views
0

我有对象gAllMedicalFilesClaimantsArray与2-属性(用户ID &的UserInfo)什么是检查使用jquery或Javascript

例如阵列阵列中是否存在特定的用户ID的最快方式:

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

使用Jquery或Javascript检查数组中是否存在特定UserID的最快方法是什么,因为gAllMedicalFilesClaimantsArray已经有8000条记录?

感谢

+0

从HTTP:/ /jquery.com/:“jQuery是一个快速简洁的JavaScript库”...如果您使用jQuery遍历数组,您实际上正在使用一个JavaScript – 2011-03-15 16:50:16

+0

@PiotrSalaciak我是否也使用JavaScript? – Raynos 2011-03-15 16:57:28

+0

对不起,但我必须问:您没有向使用此页面的每个用户发送8000+用户的阵列,对吗? – some 2011-03-15 17:31:53

回答

0
ExistsInArray(value, array){ 
    for(var item in array){ 
     if(item.UserId == value){ 
      return true; 
     } 
    } 
    return false; 
} 
+0

'for in'在性能上糟糕透了(对于JavaScript)。使用常规循环技术更好。 – Haochi 2011-03-15 18:01:04

1

这样的事情,我相信:

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)。你的例子表明它是。我在网上发现了一个在JavaScript中使用二进制搜索的良好实现,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; 
} 
+0

如果你有时间,无论如何都会看到实现。 – 2011-03-15 16:49:36

+0

你可以做一个for(var数组),而不是无论如何都不需要k。如果你想知道它存在的确切点,那么让k在那里会有好处,尽管 – Harold 2011-03-15 16:53:43

+1

@Harold a在检查时速度较慢。一个标准的前向循环会比后向循环更快。 – Raynos 2011-03-15 16:56:30

1
var match = '222'; 
var matches = $.grep(myArray, function(el, index) { 
    return (el.UserID === match); 
}); 
1

您可以通过该数组排序使用二进制搜索算法固定搜索过程(例如对于用户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; 
}; 

,可以看到其中的ID通过使用函数是12用户:

var result = binarySearch(gAllMedicalFilesClaimantsArray, 12); 
+0

好的想法。我在 – Harold 2011-03-15 16:54:37

-1

您可以原型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'); 
+0

这个问题中错过了'最快'的方式。改变内置对象的原型是一种非常糟糕的做法,因为您更改了该类型的所有对象。将函数直接添加到'gAllMedicalFilesClaimantsArray'会好得多。 (这个“规则”有一个例外:如果你添加了当前javascript引擎缺少的标准功能,但这就是jquery和类似库的用处) – some 2011-03-15 20:47:06

+0

@some真正的问题是当你扩展Object.prototype时,因为你必须过滤出继承的属性(扩展名)。只要你不使用for循环遍历一个数组,这是不推荐的,因为不能保证属性的顺序。 – 2012-07-27 04:20:31

+0

@JuanMendes对。但是,像这样扩展内置对象就像使用全局变量一样:它通常有效,但通常有更好的方法来实现它。关于在Object.prototype中添加内容:使用Object.defineProperty,应该可以添加不可枚举的属性。斯卡里。 – some 2012-07-27 04:50:35

相关问题