3

I currently have the following code which produces an error with the keys variable retrieving the json value.

var data;
var store;
for (var a in keys){
  for(var b in person){
    data = person[b].keys[a];
    if (data=="1"){
      store += "hit ";
    }
  }
}

The json object im testing with looks like:

var person = [
  {
    "Permissions": "Admin",
    "Scope": "Super powers",
    "ReadOnly User": "stuff",
    "Admin User":"1"
  },
  {
    "Permissions": "Read-Only",
    "Scope": "Reading",
    "ReadOnly User": "some stuff",
    "Admin User":"0"
  },
  {
    "Permissions": "Do Guy",
    "Scope": "Labour",
    "ReadOnly User": "many things",
    "Admin User":"1"
  }
];

and keys are retrieved with the following:

var keys =[];
   if(person.hasOwnProperty(0)){
      for(var prop in person[0]){
         if(person[0].hasOwnProperty(prop)){
           if(prop !== 'Permissions' && prop !== 'Scope'){
             keys.push(prop);
           }
         }
      }
   }

The end result of this should have two hits in store for each Admin User key with a 1

Daxxcat
  • 163
  • 1
  • 5
  • 1
    what is `store` supposed to be? – Paul Fitzgerald Aug 24 '16 at 16:27
  • 1
    Have you [checked your console for errors?](http://stackoverflow.com/documentation/javascript/185/hello-world/714/using-console-log) You [shouldn't be using `for..in`](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) on an array and each element does not have a property named "keys". Extra note: You don't have a "JSON object." You have an array. There's no such thing as a "JSON object." JSON is just a text format used to encode data. It stands for "Javascript Object Notation" so any valid JSON is valid JS object syntax. – Mike Cluck Aug 24 '16 at 16:28
  • Store is just a test variable at the moment storing a "hit" every time a match comes up. The final version has working code in the if condition. – Daxxcat Aug 24 '16 at 16:28
  • each element doesn't have a property named "keys" but it does have a property of stored in the keys array. which looks like [ReadOnly User, Admin User] – Daxxcat Aug 24 '16 at 16:37
  • @Daxxcat No, it doesn't have a property of `keys` at all. If you want to access something *from* your `keys` array, consider doing `person[a][keys[b]]`. – Mike Cluck Aug 24 '16 at 16:39

2 Answers2

0

Another way to do this might be to do two passes: one to filter down to the list of items you want to work on (eg: find the admin users) and then another to do the actual work (eg: record a "hit")

var person = [{
  "Permissions": "Admin",
  "Scope": "Super powers",
  "ReadOnly User": "stuff",
  "Admin User": "1"
}, {
  "Permissions": "Read-Only",
  "Scope": "Reading",
  "ReadOnly User": "some stuff",
  "Admin User": "0"
}, {
  "Permissions": "Do Guy",
  "Scope": "Labour",
  "ReadOnly User": "many things",
  "Admin User": "1"
}];

person
  .filter(function (user) {
    return user['Admin User'] === "1";
  })
  .forEach(function (admin) {
    // here, do what you need to do with the admin users
    console.log(admin);
  })

Edit: I just saw your comment on a different answer: "Script needs to be generic and not to a specific key"

function filterBy(arr, key, val) {
  return arr.filter(function (item) {
    return item[key] === val;
  });
}

filterBy(person, 'Admin User', '1'); // gives a list of the admin users
nickf
  • 537,072
  • 198
  • 649
  • 721
  • Thanks, that isn't a bad idea but how would i do this if I don't know what the keys in the json are going to be? Sorry for not mentioning but my case requires it be generic for a changing json. Thus the need for the keys array I've shown the code snippet for. – Daxxcat Aug 24 '16 at 16:44
  • so what's the condition you're looking for? If you don't know what keys there are, are you just going to check every field for some value? Doesn't sound quite right. – nickf Aug 24 '16 at 17:08
  • Thank you very much. This solved my issue and faster than the original loop I was doing. I'll be able to return all the searched data in one shot instead of many iterations to find. I can use this function to search by my keys array, by calling the function from a loop. – Daxxcat Aug 24 '16 at 17:29
0

Look closely at this line:

data = person[b].keys[a];

This says to take the property b of the person object, then, take its property "keys", then take its property a.

Obviously, person[b] has no property named "keys". You are looking for

data = person[b][keys[a]];

In other words, you are taking the property named by a from the property named "keys" from the property named as b from person.

What you want to do is take the property named as the property named as a from the array named keys from the property named b from person.