0

I need help in fetching values from a response object in an ajax call

Code Snippet

$.each(responseJson.slice(0,7), function (index) {
var resp_JSON=responseJson[index];
console.log(resp_JSON);

In console the resp_JSON is Object {17130003: "A message string from the cache"}

Now the response Json doesn't has a name tag so that I can do resp_JSON.id or something & get the value. It just has values.

I tried

resp_JSON[0]; //Error

resp_JSON.Object[0]; //Error

I need to fetch 17130003 & A message string from the cache in two separate javascript variables.

underdog
  • 4,447
  • 9
  • 44
  • 89

1 Answers1

1

In order to get the keys and values of the object. You can do this:

var keys = Object.keys(resp_JSON); // [17130003]
var values = Object.values(resp_JSON); // ["A message string from the cache"]

Note that both are arrays, and you can simply loop through the array to handle each value/key.

Also, as @Hassan has pointed out, you can get the specific value by resp_JSON['some_key'] if that's what you want to achieve.

Rax Weber
  • 3,730
  • 19
  • 30