1

I'm not 100% clear how to formulate a $.each() function.

The following JSON data block contains multiple items in the array ItemValues. What I'm looking for is a method (because this packet can be arbitrarily large) is somewhat to get each ItemValue array item WHERE Fields.Id is equivalent to some value "x".

As well, is there some method to retrieve ItemValues[n].Fields.units as an array?

JSON:

{
   "CacheName":"Default",
       "LastUpdateTime":"\/Date(1396994130568)\/",
       "Type":"Start",
       "TotalItemsCount":3,
       "PassingFilterCount":3,
       "ItemValues":[
      {
         "Fields":{
            "id":"13288263",
            "status":"8",
            "x_cord":"7250600",
            "y_cord":"566620200",
            "code":"1021",
            "sub_code":"1W",
            "priority":"1",
            "units":"1011, 1130, 1201, 1233, 1445, 1501, 1518, 1714, 1726, 1823, 1825, 1832, 3662",
            "ts":"20140402234025UT"
         },
         "Id":"13288263",
         "LastAction":"add"
      },
      {
         "Fields":{
            "id":"13288264",
            "status":"8",
            "x_cord":"",
            "y_cord":"",
            "code":"",
            "sub_code":"",
            "priority":"-1",
            "units":"",
            "ts":""
         },
         "Id":"13288264",
         "LastAction":"add"
      },
      {
         "Fields":{
            "id":"13288265",
            "status":"8",
            "x_cord":"",
            "y_cord":"",
            "code":"",
            "sub_code":"",
            "priority":"-1",
            "units":"",
            "ts":""
         },
         "Id":"13288265",
         "LastAction":"add"
      }
    ]
}
Community
  • 1
  • 1
SteveMustafa
  • 621
  • 2
  • 8
  • 19
  • You might want to have a look at http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json to learn how to process nested data structures. Then you can switch to `$.each` if you want to. – Felix Kling Apr 09 '14 at 21:52
  • @FelixKling Thanks Felix, I'm aware of how to traverse data that way, I'm specifically looking for a $.each manner because I don't want to loop several times over the structure checking for a specific field equivalence. – SteveMustafa Apr 09 '14 at 22:02
  • 1
    `$.each` is just a substitute for `for` and `for...in` loops. It doesn't do anything that loops can't do. E.g. `$.each(d, function(k, v) {})` is the same as `for (var k = 0; k < d.length; k++) { var v = d[k]; }` or `for (var k in d) { var v = d[k]; }`. If you know how to do it without `$.each`, you know how to do it with `$.each`. – Felix Kling Apr 09 '14 at 22:03
  • 1
    use linqjs it wins the game... https://linqjs.codeplex.com/ – undefined Apr 09 '14 at 22:06
  • @FelixKling I don't know what it was you said in that comment, but I re-read that and something just clicked in my brain and its all clearer now. Sometimes, I'd like to kick myself. Thanks and sorry if I seemed ungrateful in my first reply! – SteveMustafa Apr 09 '14 at 22:09
  • No worries, it's all fine :) Just wanted to make sure you know what `$.each` does ;) Happy coding! – Felix Kling Apr 09 '14 at 22:10
  • You can use .filter on arrays – Brent Echols Apr 09 '14 at 23:22
  • Did any solution worked for you? – Maen Apr 15 '14 at 13:43
  • Yes. I apologize, I forgot to mark the correct answer. – SteveMustafa Apr 15 '14 at 14:34

2 Answers2

1

Assuming your json shown is in variable json, then for this exact scenario you could use:

function validItems(id,json){
 var validItemValues = [];
 var values = json.ItemValues;
 for(var value in values){
  if( !values.hasOwnProperty(value) )continue;
  var itemValue = values[value];
  if( itemValue.Fields.id == id ){
   validItemValues.push(itemValue);
  }
 }
 return validItemValues;
}

This same method using jQuery's .each:

function validItems(id,json){
 var validItemValues = [];
 $.each(json.ItemValues,function(){
  if(this.Fields.id == id)validItemValues.push(this);
 });
 return validItemValues;
}

For a recursive approach to finding json values, see this answer: https://stackoverflow.com/a/11657379/1026459

Once you have the valid items, you can iterate through them, and then convert the units string to an array using split.

var validItemList = validItems(id,json);
for( var item in validItemList ){
 if( !validItemList .hasOwnProperty(item ) )continue;
 var currentItem = validItemList[item];
 var fieldsUnitArray = currentItem.Fields.units.split(" ");
 //work with array of units
}
Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273
1

Here's a code which shows it all (using $.each() as you wish), using JSON.parse() and String.split()

var json_string = "Your JSON here";

//Use JSON.parse to make it an object
var json_object = JSON.parse(json_string.innerHTML);

//Will store the matched identifier
var matched_id;

//Will store the matched array
var matched_array;

//A valid test case, with units not null
var x = 13288263; 
$.each(json_object.ItemValues, function(key, item_value){
    if(item_value.Fields.id == x){
        matched_id = item_value.Fields.id;

        //Second part of the question
        var units = item_value.Fields.units;
        if(units != ""){
            //String.split() returns an array, by splitting the string with 
            //selected string
            matched_array = units.split(', ');
        }
    }
});

Here's a demo, to play with it.

Note : there surely are some better solutions out there.

Maen
  • 10,603
  • 3
  • 45
  • 71