0

Okay, so i'm aware there are similar questions on this on the site BUT all I have seen have people with an array INSIDE an array. So I'm just looking for a bit of help on a simple one that I just can't seem to grasp.

The JSON is being generated from a PHP file and looks something like this when retrieved via AJAX: {'name' : 'Derick', 'email' : '[email protected]'}.

How can I loop through this response, using Javascript, to retrieve the value for, say, the 'name' key in the array?

code snippet:

response = {'name' : 'Derick', 'email' : '[email protected]'};
3
  • 4
    it's an object not an array, you don't need to loop, just response.name or response['name'] should do it Commented Apr 7, 2020 at 20:06
  • 2
    The reason you haven't seen questions like this is because there's nothing complicated about it. It's just ordinary object access. Commented Apr 7, 2020 at 20:08
  • Yes, this was my assumption when using it to get responses from the server and so had already tried response.name and it wasn't working. Still isn't. I have even tried parsing the response before calling but this still isn't working. I have just tried response['name'] and it also isn't working. Commented Apr 7, 2020 at 21:36

3 Answers 3

1

If the response is only the given object, you can clearly get it by response['name'].

If the response that you recieve is an array of objects like the one you wrote:

let result = [];
response.forEach(e => result.push(e['name']));

Should do what you want.

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

Comments

1

Your example code snippet is not an object array, but just an object. You would not loop thru it to access the data. But rather, access the data like this:

response.name and response.email

If your json object was an array, it would look something like this:

responses = [
    {
      'name' : 'Derick', 
      'email' : '[email protected]'
    },
    {
      'name' : 'John', 
      'email' : '[email protected]'
    }    
]

And you can loop through it such as:

for (x of responses) {
  console.log(x.name + ' ' + x.email);
}

Comments

0

Okay, after trying all these suggestions, nothing worked. Can't understand why. So the solution that has worked for me is this, (You can also find it on W3Schools, under 'Javascript JSON parse() method).

JSON.parse(response, function (key, value) {
  if (key == "name") {
      console.log(value);
  }
});

So basically, the code parsed the response (even though my response should have come back parsed. Redundant, I know) to a javascript object and loops through said object to find they key-value pair you're looking for.

This has worked for me although I have been unable, for the life of me, to deduce what may be wrong with simply using response.name or response['name'] to access them.

Anyway, hope this helps. A big thank you to all who took time out of their day to reach out to help a junior dev.

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.