This is my first post : I've been looking for a solution for quite a long time.

So I have this JSON 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? Any help would be greatly appreciated! :>

share|improve this question
feedback

3 Answers

up vote 2 down vote accepted

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");
share|improve this answer
thanks! I had to loop through another level, but it works now :) – davidgmar Feb 10 '12 at 3:21
feedback

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);
share|improve this answer
1  
+1 for function filter – diEcho Feb 9 '12 at 8:12
feedback
var result = [];
for (var i = 0; i < object.length; i++)
{
    if (object[i].name == 'Fran')
    {
        result.push(object[i]);
    }
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.