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.

2
  • 3
    Actually you need to access resp_JSON['17130003'], if the keys are dynamic you can use Object.values(resp_JSON) and Object.keys(resp_JSON) Commented Aug 2, 2017 at 6:18
  • 1
    @HassanImam Thanks buddy it worked. Commented Aug 2, 2017 at 6:22

1 Answer 1

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.