2012-02-09 14 views
8

To jest mój pierwszy wpis: Szukałem rozwiązania od dość dawna.Jak filtrować wielowymiarową tablicę javascript

Mam więc te dane 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 
    }}]; 

I chciałbym, aby filtrować je według nazwy. Na przykład, jeśli stosuje się filtr o nazwie „Fran”, chciałbym mieć coś takiego:

[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! 
    ) 

jest to możliwe do osiągnięcia? Każda pomoc będzie bardzo ceniona! :>

+0

Możesz być zainteresowany moim interfejsem API JSON.search, który jest 5x szybszy niż $ .grep i umożliwia wyszukiwanie za pomocą wyrażeń regularnych. Zobacz http://json.spiritway.co/ – mgwhitfield

Odpowiedz

8

Nie ma dla tego funkcji w JavaScript. Musisz napisać własną funkcję w ten sposób.

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

dzięki! Musiałem przejść przez inny poziom, ale teraz działa :) – davidgmar

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

by utworzyć funkcję filtrowania:

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; 
} 

bardziej solidne rozwiązanie może być dodanie sposób filtr z prototypem:

`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; 
    }; 
} 

Następnie po prostu zadzwoń:

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

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

+1 dla funkcji 'filter' – diEcho

0

Największy trik polega na zrobieniu płaskiej macierzy z elementem, który w niej chcesz.

że masz 2d array tak:

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

zrobić nową tablicę:

var f = [] 

napełnić go tylko 1 sztukę:

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

dając nam polubienia:

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

a potem ....

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

Cowabunga!

Powiązane problemy