2010-09-02 103 views
1

如何寻找数组中对象的属性,而不使用JavaScript中for循环?在JavaScript数组搜索对象

如果数组是一个简单的,我可以用array.indexOf(value)得到索引,但如果数组对象的数组是什么?除了循环其他方式吗?

例如,ar = [{x,y},{p,q},{u,v}]。如果搜索v,它应该将数组索引返回为2.

回答

3

在数组中搜索值通常需要sequential search,要求您循环每个项目直到找到匹配项。

function search(ar, value) { 
    var i, j; 
    for (i = 0; i < ar.length; i++) { 
    for (j in ar[i]) { 
     if (ar[i][j] === value) return i; 
    } 
    } 
} 

search([{'x': 'y'}, {'p': 'q'}, {'u': 'v'}], 'v'); // returns 2; 
+0

是的,这样我明白了。我想如果有一些内置函数来执行搜索,就像我们可以使用array.sort()和compare函数根据对象属性对数组进行排序。 – sat 2010-09-02 07:56:48

+0

@sat:不是。这些是可用于数组的方法:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array。在JavaScript 1.6中有'filter()'方法,但对于您的需求来说这仍然太复杂。 – 2010-09-02 08:07:41

+0

感谢您的信息。 – sat 2010-09-02 08:52:14

0

Searching for objects in JavaScript arrays

javascript: 
    /* quick fix naive short solution to be posted soon */ 
    /* array of objects with primitive property values only and no wrinkles */ 

javascript: 
    alert(
     JSON.stringify(
     [{x:1,y:2},,,{p:"q"},{u:{},vx:[],x:{y:{},x:5}}] 
      ) . match(/"x":/g) 
    ) 

javascript: /* Does the need to fortify this code imply JSON is stronger? */ 
    alert(           /* See wrinkles below */ 
     [{x:1,y:2},,,{p:"q"},{u:{},vx:[],x:{y:{},x:5}}] . toSource() . 
/* 
     match(/({|,)\s*x:/g) . join() . replace(/({|,)\s*x:/g,"x:") 
    finds `x:,x:,x:` 
*/ 
     replace(/(({|,)\s*)([^,{:]*):/g,'$1"$3":') . match(/"x":/g) 
    ) 

找到"x":,"x":,"x":

特定的属性发现,木已成舟?

暗示,暗示(但必须适当衰减,截肢嵌套动物):

javascript: 
    alert(
     JSON.stringify([{x:1,y:2},{p:"q"},{u:{},v:[],x:{y:{},x:5}}]) . 
      match(/"[^"]*":/g) 
    ) 

发现"x":,"y":,"p":,"u":,"v":,"x":,"y":,"x":(?所有属性 - 现在完成)

更多(多很多)脑应变疼痛会发现x:数组的位置值和索引(提示数顶级,'s)。

截肢和衰减提示(只删除嵌套阵列和对象,的,见皱纹):

javascript:debug=false; 
    animal=[ 
     {x:1,y:2},,,{p:"q"}, 
     [ {u:{},vx:[,,], x:{y:{xx:''},x:5} }, "hmmm comma x colon \" monster" ], 
    ]; 
    animal=animal.toSource().replace(/\[(.*)]/,"$1"); 
/* */ if(debug){ 
    alert(animal); 
    animal=animal.replace(/\[([^[\]]*)\]/g, 
       function(a,b,c,d){alert([a,b,c,d].join("\n\n"));return a}); 
    while(animal.search(/\{.*\}|\[.*\]/)>-1){ 
     animal=animal.replace(/\{([^{}]*)\}|\[(.*)\]/g, 
     function(a,b,c,d){alert([a,"\n",b,"\n",c]);return b.replace(/,/g,";")}); 
     alert(animal); } 
/* */ } 

    /* the while loops on nesting depth not top array length */ 
    while(animal.search(/\{.*\}|\[.*\]/)>-1) 
     animal=animal.replace(/\{([^{}]*)\}|\[(.*)\]/g,  /* implicit g loop */ 
        function(a,b,c,d){return (b+c).replace(/,/g," ")}); /* ditto */ 
    alert(animal); /* as opposed to a non-lert animal? */ 

皱纹:

  • .toSource()更强(但...见上文)和处理比JSON更多的情况
    ref:Implementing Mozilla's toSource() method in Internet Explorer

  • 如果有包含字符串的怪物会怎么样:
    1. ,的s。 。 。如在。 。 。 [",,or",,{p:"1,2,3,"}]
    2. {x:...}{"x":...}。 。 。如在。 。 。 ['{"x":...}'," and ","{x:...}",,]
      (将弄糟上述编码使用任一JSONtoSource
    3. 嵌套怪物
    4. 其它畸形仅仅是嵌合体...没有足够的支付做或证明