1

I have the following json array:

{"0":  
   {"ChkSystem":
      {"id":"847",
       "item":"1",
       "fase":"#FE99CC",
       "description":"some info.",
       "image_path":"",
       "pm_id":"461",
       "main_systems_id":"1"
      }
   },
 "1":
   {"ChkSystem":
      {"id":"846",
       "item":"1",
       "fase":"#FE99CC",
       "description":"some data",
       "image_path":"",
       "pm_id":"461",
       "main_systems_id":"2"
      }
   },
"2":
  {"ChkSystem":
     {"id":"856",
      "item":"2",
      "fase":"#FE99CC",
      "description":"some data.",
      "image_path":"",
      "pm_id":"461",
      "main_systems_id":"2"
     }
  }
}  

How can I filter it by "main_systems_id" with jquery?
The problem is the key is variable ("0", "1" and so on) and in other posts that I had read that key is always the same.

Thanks in advance for your answers.

5
  • filter it how exactly ? Commented Jun 10, 2014 at 22:40
  • 1
    Just out of curiosity... why you use "0", "1", "2" object properties instead of using an Array of objects? Array is already indexed. Commented Jun 10, 2014 at 22:47
  • Hi @adeneo, for example posted above, I want all "ChkSystem" arrays where "main_systems_id" are equal to "2" Commented Jun 10, 2014 at 22:50
  • Hi @RokoC.Buljan the array is in this form because is a php array generated by a cakephp query Commented Jun 10, 2014 at 22:54
  • @Efraín if your cakephp query was setup by a human... he was most probably in love... Commented Jun 10, 2014 at 22:57

2 Answers 2

1

I think a tool like lodash or underscore is more suited for something of this nature.

However should you really want to do it with jQuery:

function filterByMainSystemsId (object, value) {

  // The real meat of the solution, you can use this directly if you want.
  return $.map(object, function (item, key) { 

      // this is where the check is done
      if (item.ChkSystem.main_systems_id === value) {

        // if you want the index or property "0", "1", "2"... etc.
        // item._index = key;

        return item; 
      }
    });
  };
}

var object = /* ... your object goes here ... */;
var array = filterByMainSystemsId(object, "2") // [ { ... }, { ... } ]

I agree however, this should be an array and not an object, but things like elasticsearch and other tools make this hard and I can understand that.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Nijikokun thank for your answer, your code work perfectly but how can I do to get the object in plain object? I mean the form I posted in my question {{...}}
I've created a jsfiddle showing how you could do this here jsfiddle.net/Nijikokun/J8A68 instead of returning the array from $.map, just add each item to an object and return the object instead.
0

This is not an array, but an object, containing sub objects. This would be the code for a json array:

[
{"ChkSystem":
      {"id":"847",
       "item":"1",
       "fase":"#FE99CC",
       "description":"some info.",
       "image_path":"",
       "pm_id":"461",
       "main_systems_id":"1"
      }
   },
   {"ChkSystem":
      {"id":"846",
       "item":"1",
       "fase":"#FE99CC",
       "description":"some data",
       "image_path":"",
       "pm_id":"461",
       "main_systems_id":"2"
      }
   },
  {"ChkSystem":
     {"id":"856",
      "item":"2",
      "fase":"#FE99CC",
      "description":"some data.",
      "image_path":"",
      "pm_id":"461",
      "main_systems_id":"2"
     }
  }
]

but you can also loop through json objects (key value pairs) and furthermore filter them. See StackOverflow: loop and get key/value pair for JSON array using jQuery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.