How to filter multidimensional JavaScript array

There is no function for this in Javascript. You have to write your own function like this.

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");

The question is about multidimensional arrays. If you like me missed that here are solutions for normal arrays...

2020

filteredArray = array.filter(item => item.name.indexOf('Fran') > -1);

or

filteredArray = array.filter(function(item)
{
    return item.name.indexOf('Fran') > -1);
}

2012 version

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

I would create a function for filtering :

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

A more robust solution might be adding a filter method to the prototype:

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

Then simply call:

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

var result = object.filter(filterName);