2012-02-09 70 views
8

這是我的第一篇文章:我一直在尋找一個相當長的解決方案。如何過濾多維JavaScript數組

所以我有這樣的JSON數據:

var object = [{ 
    "nid": "31", 
    "0": { 
     "tid": "20", 
     "name": "Bench Press", 
     "objectDate": "2012-02-08", 
     "goal": "rep", 
     "result": "55.00", 
     "comments": "sick!", 
     "maxload": "250" 
    }, 
    "1": { 
     "tid": "22", 
     "name": "Back Squat", 
     "objectDate": "2012-02-08", 
     "goal": "time", 
     "result": "8.00", 
     "comments": "i was tired.", 
     "maxload": "310" 
    }}, 
{ 
    "nid": "30", 
    "0": { 
     "tid": "19", 
     "name": "Fran", 
     "objectDate": "2012-02-07", 
     "goal": "time", 
     "result": "5.00", 
     "comments": null 
    }}]; 

而且我想通過名字來過濾。舉例來說,如果我申請一個過濾器的名稱「弗蘭」,我想是這樣的:

[0] => Array 
    (
     [tid] => 19 
     [name] => Fran 
     [objectDate] => 2012-02-07 
     [goal] => time 
     [result] => 5.00 
     [comments] => 
    ) 
[1] => Array 
    (
     [tid] => 19 
     [name] => Fran 
     [objectDate] => 2012-02-08 
     [goal] => rep 
     [result] => 55.00 
     [comments] => woohoo! 
    ) 

是否有可能實現嗎?任何幫助將不勝感激! :>

+0

您可能對我的JSON.search API感興趣,它的速度比$ .grep快5倍,並允許您使用正則表達式進行搜索。請參閱http://json.spiritway.co/ – mgwhitfield 2015-05-08 20:48:20

回答

8

在Javascript中沒有此功能。你必須寫下你自己的功能。

var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}]; 


function filterByProperty(array, prop, value){ 
    var filtered = []; 
    for(var i = 0; i < array.length; i++){ 

     var obj = array[i]; 

     for(var key in obj){ 
      if(typeof(obj[key] == "object")){ 
       var item = obj[key]; 
       if(item[prop] == value){ 
        filtered.push(item); 
       } 
      } 
     } 

    }  

    return filtered; 

} 

var byName = filterByProperty(arr, "name", "Fran"); 
var byGoal = filterByProperty(arr, "goal", "time"); 
+0

謝謝!我不得不循環通過另一個級別,但它現在工作:) – davidgmar 2012-02-10 03:21:18

5
var result = []; 
for (var i = 0; i < object.length; i++) 
{ 
    if (object[i].name == 'Fran') 
    { 
     result.push(object[i]); 
    } 
} 
5

我將創建一個功能,用於濾波

function filter(array, key, value){ 
    var i, j, hash = [], item; 

    for(i = 0, j = array.length; i<j; i++){ 
     item = array[i]; 
     if(typeof item[key] !== "undefined" && item[key] === value){ 
      hash.push(item); 
     } 
    } 

    return hash; 
} 

更健壯的解決方案可能是增加一個過濾器的方法的原型:

`This prototype is provided by the Mozilla foundation and 
    is distributed under the MIT license. 
    http://www.ibiblio.org/pub/Linux/LICENSES/mit.license` 

if (!Array.prototype.filter) 
{ 
    Array.prototype.filter = function(fun /*, thisp*/) 
    { 
    var len = this.length; 
    if (typeof fun != "function") 
     throw new TypeError(); 

    var res = new Array(); 
    var thisp = arguments[1]; 
    for (var i = 0; i < len; i++) 
    { 
     if (i in this) 
     { 
     var val = this[i]; // in case fun mutates this 
     if (fun.call(thisp, val, i, this)) 
      res.push(val); 
     } 
    } 

    return res; 
    }; 
} 

然後只需撥打:

function filterName (item, index, array) { 
    return (item.name === "Fran"); 
} 

var result = object.filter(filterName); 
+1

+1以獲取函數'filter' – diEcho 2012-02-09 08:12:54

0

最大的竅門就是製作一個平面陣列,只需要你想要的物品。

說你有一個二維數組,像這樣:

e = [[1,2],[1,2],[2,3],[4,5],[6,7]] 

你犯了一個新的數組:

var f = [] 

只有1項填寫:

for(var x=0;x<e.length;x++) f[x] = e[x][1] 

給我們喜歡的:

f = [2,2,3,5,7] 

,然後....

var g = e.filter(function(elem, pos){ return (f.indexOf(elem[1]) == pos) }) 

cowabunga!