Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter multidimensional JavaScript array

I have this data:

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

and I would like to filter it by name. For instance, if I apply a filter for the name "Fran", I'd like to have something like this:

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

Is it possible to achieve?

like image 984
davidgmar Avatar asked Feb 09 '12 07:02

davidgmar


People also ask

How do you filter an array in JavaScript?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

Does JavaScript support multidimensional array?

Javascript has no inbuilt support for multidimensional arrays, however the language is flexible enough that you can emulate this behaviour easily by populating your arrays with separate arrays, creating a multi-level structure.

How multidimensional array works in JavaScript?

Multidimensional arrays are not directly provided in JavaScript. If we want to use anything which acts as a multidimensional array then we need to create a multidimensional array by using another one-dimensional array. So multidimensional arrays in JavaScript is known as arrays inside another array.


1 Answers

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");
like image 120
Diode Avatar answered Sep 21 '22 15:09

Diode