如何过滤多维javascript数组

dav*_*mar 11 javascript arrays json filter multidimensional-array

这是我的第一篇文章:我一直在寻找解决方案很长一段时间.

所以我有这个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
    }}];
Run Code Online (Sandbox Code Playgroud)

我想按名称过滤它.例如,如果我为名称"Fran"应用过滤器,我想要这样的东西:

[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!
     )
Run Code Online (Sandbox Code Playgroud)

有可能实现吗?任何帮助将不胜感激!:>

Dio*_*ode 10

在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");
Run Code Online (Sandbox Code Playgroud)


PiT*_*ber 8

var result = [];
for (var i = 0; i < object.length; i++)
{
    if (object[i].name == 'Fran')
    {
        result.push(object[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ric*_*d A 7

我会创建一个过滤功能:

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;
}
Run Code Online (Sandbox Code Playgroud)

更强大的解决方案可能是为原型添加过滤方法:

    `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;
  };
}
Run Code Online (Sandbox Code Playgroud)

然后简单地致电:

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

var result = object.filter(filterName);
Run Code Online (Sandbox Code Playgroud)