0

Im working on parsing a json object array so that if the value = 'y', the list of keys is returned as a comma-seperated list that is a variable that will be returned in html.

I want to reference the key/value pairs by index position because I only want to return the values that are the key/values #13-39.

Any suggestions about the best way to go about this is?

I already have a jquery map function to access some of the properties through (ie, obj.address). But I havent been able to successfully use filter or grep to only select the object properties with certain key names or index positions.

This is what the array looks like:

[{"Name": "Name1", "Key1": "Y", "Key2": "Y", "Key3": "N", "Key4": "Y", "Key5": "N"},
{"Name": "Name2", "Key1": "N", "Key2": "Y", "Key3": "Y", "Key4": "N", "Key5": "N"},
{"Name": "Name3", "Key1": "N", "Key2": "Y", "Key3": "Y", "Key4": "Y", "Key5": "Y"}}

Thanks for any help!

Pointy
  • 405,095
  • 59
  • 585
  • 614
sharpiemarker1
  • 305
  • 3
  • 6
  • 12
  • Which value should be "Y"? You've provided several in your example code. What should be "Y" in order for you to render the data in the manner you express? – Samir Talwar Jun 20 '11 at 23:09
  • Any of the key's whose value is 'y' should be displayed. So the comma-seperated list would be, ie for the 1st object in the array: "Key1, Key2, Key4" – sharpiemarker1 Jun 20 '11 at 23:12
  • What would be the expected output for the given example array? Should it be something like: `Name1: Key1, Key2, Key4; Name2: Key2, Key3; Name3: Key2, Key3, Key4, Key5`? – Xint0 Jun 20 '11 at 23:13
  • Each Name/object is being displayed as a seperate page in jquery mobile. The pages are being created through jQuery's each. So the list of keys will be tied to the Name/object's page and accessible as a variable, ie "keys", that will be formatted & displayed within the each function. – sharpiemarker1 Jun 20 '11 at 23:21

1 Answers1

0

I suppose you can figure out some way to do this using jQuery's $.map, but personally I just find it easier to think of in a pure JavaScript way. (Note that if your keys aren't actually named "Key1", "Key2", etc. but you want to access the keys by their position in an associative array, this has no reliable cross-browser implementation.):

var bar = [
  {"Name": "Name1", "Key1": "Y", "Key2": "Y", "Key3": "N",
                    "Key4": "Y", "Key5": "N"},
  {"Name": "Name2", "Key1": "N", "Key2": "Y", "Key3": "Y",
                    "Key4": "N", "Key5": "N"},
  {"Name": "Name3", "Key1": "N", "Key2": "Y", "Key3": "Y",
                    "Key4": "Y", "Key5": "Y"}
];
var parsed = {}; // this contains the result
var MIN_KEY = 2; // set to 13 for your request
var MAX_KEY = 4; // set to 39 for your request

for (var i = 0, iLen = bar.length; i < iLen; i++) {
  var baz = bar[i];
  var name;
  var temp = [];
  for (var key in baz) {
    var keyNum = parseInt(key.replace("Key", ""));

    // Get the name or add valid key to an array
    if (key === "Name") {
      name = baz[key];
    } else if (baz[key] === "Y" && keyNum >= MIN_KEY && keyNum < MAX_KEY) {
      temp.push(key);
    }
  }

  // Concatenates temp array like "Key1, Key2, Key3"
  parsed[name] = temp.join(", ");
}

console.log(bar);
console.log(parsed);

Alternative implementation by counting keys (note that this may not be reliable across browsers):

for (var i = 0, iLen = bar.length; i < iLen; i++) {
  var counter = 0;
  var baz = bar[i];
  var name;
  var temp = [];
  for (var key in baz) {
    if (key === "Name") {
      name = baz[key];
    } else if (baz[key] === "Y" && counter >= MIN_KEY && counter < MAX_KEY) {
      temp.push(key);
    }
    counter++;
  }
  parsed[name] = temp.join(", ");
}
brymck
  • 7,555
  • 28
  • 31
  • Ok - yeah - they're not actually named Key1, Key2 etc. So there is no reliable cross-browser way to access them by index position? – sharpiemarker1 Jun 21 '11 at 00:31
  • Not really. [Here's some more info.](http://stackoverflow.com/questions/6316618/do-javascript-arrays-and-objects-have-a-set-order/6316786#6316786) You can use the `for` loop above and use a counter variable (see my edit above), then test against that if you want to, just keep in mind that there's no real standard and things may not work. – brymck Jun 21 '11 at 00:38
  • Ok - thanks a lot for that link. I was wondering why it seemed like things kept coming back semi-randomly... now I know. Thanks a lot for yr help. – sharpiemarker1 Jun 21 '11 at 00:51