0

I'm trying to get the key values of each record in a JSON array when looping through it. Currently I have a simple JSON object like this:

 "users": {
    "key_11": {
      "text": "11"
    },
    "key_22": {
      "text": "22"
    },
    "key_33": {
      "text": "33"
    }
 }

My current script uses the 'map' method to convert this JSON objet to a loop-able array:

var user_profiles_array = $.map(user_profiles_string, function(el) { return el; });

for (var xt = 0; xt < user_profiles_array.length; xt++) {
    console.log(user_profiles_array[xt].text); //11 or 22 
}

My question is, how can I get the value for e.g: 'key_11' or 'key_22'?

Thanks!

5
  • users.key_11.text is the value of the "text" property of the "key_11" object. Commented Jan 20, 2016 at 15:53
  • Yes, but I need a way to get 'key_11' and 'key_22' and 'key_33' etc, not the value for the text property Commented Jan 20, 2016 at 15:54
  • The Object.keys() function returns an array of property names for a given object. Commented Jan 20, 2016 at 15:54
  • Using this function I get [0,1,2,3,4,5], but not [key_11, key_22, key_33] Commented Jan 20, 2016 at 15:57
  • Object.keys(YOUR_OBJECT_VARIABLE.users). Look: jsfiddle.net/uxLug8pu Commented Jan 20, 2016 at 15:59

2 Answers 2

2

you can use Object.keys to get an array of all of your object's keys. Once you have that array, you can use Array.forEach to iterate over it as necessary:

Object.keys(usersObject).forEach(function(key, keyIndex) {
  console.log("index:",keyIndex,"key:",key,"value:",usersObject[key]);
});

But!

your particular problem here is being caused by using $.map instead of JSON.parse. $.map returns an array, so of course your keys are always going to be numerical array indices - 0, 1, 2, and so on. You're not going to be able to use hash keys to find things in the array returned by $.map. Furthermore, judging by your variable names you're calling $.map on a string which is definitely not going to do what you want. Assuming you figure that part out and you somehow get a valid JavaScript object, and you still need to use $.map() for some reason, what you can do is this:

// $.map will return an array...
$.map(user_profiles_object, function(objVal, objKey) {
    // ...and each item in that array will be an object with a
    // property named 'key' and a property named 'val'
    return {
      key: objKey,
      val: objVal
    };
}).forEach(function(arrayObj) {
    // now each item in the array created above will be an object
    // created by your callback function:
    console.log(arrayObj.key,":",arrayObj.val);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Is 'usersObject' the variable holding my converted array?
yes, but is there a reason you're using $.map and not JSON.parse ? because I think that's why you're seeing "0", "1", etc. as array indices instead of "key_11", "key_22", etc.
$.map is converting the object to an array - JSON.parse is just parses string JSON?
The reason I used $.map was because JSON.parse didn't work for me. I kept getting the following error: 'JSON.parse: unexpected character at line 1 column 2 of the JSON data' I'm trying to parse a Firebase JSON file and maybe it's because their file is formatted a bit different?
your JSON isn't well-formed, I bet. try wrapping your string in curly braces and then try parsing it: console.log(JSON.parse("{" + user_profiles_string + "}"));
0

You can also rely on Js's foreach.

// JSON string must be valid. Enclose your JSON in '{}' (curly braces);
var user_profiles_string =  '{ "users": { "key_11": { "text": "11" }, "key_22": { "text": "22" }, "key_33": { "text": "33" }}}';
var user_profiles_array  = JSON.parse(user_profiles_string); 

// For retrieval in loop, the Js foreach asigns the key to index param (i in this case).
for (i in user_profiles_array.users) {
    // i is the key of the user currently iterated.
    console.log('Key name is: ' + i);
    // Use i as the index to retrieve array value.
    console.log(user_profiles_array.users[i]);
}

// For direct retrieval using any given known key:
console.log(user_profiles_array.users['key_11']);

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.