-4

{status: '202', data: {…}}

data: {1: 'Sector-5', 2: 'Sector-20', 3: 'abc', 4: 'Kalka', 5: 'Pinjore', 6: 'Sector-14', 7: 'Sector-7', 8: 'MDC', 9: 'WPS', 10: 'cde', 11: 'efg'} status: "202"

how to access data object all value in jquery

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Assume you haven't tried anything? How are you receiving/calling this `json` data? – Darren Nov 02 '22 at 03:21
  • i try to access it by if (response.status == '202') { $(response.data).each(function(key,value){ // console.log(value); }); } where response = JSON.parse(result); – Amit Dhiman Nov 02 '22 at 03:37
  • [Iterating a JavaScript object's properties using jQuery](https://stackoverflow.com/q/1096924/2943403) – mickmackusa Nov 02 '22 at 05:33
  • Does this answer your question? [Iterating a JavaScript object's properties using jQuery](https://stackoverflow.com/questions/1096924/iterating-a-javascript-objects-properties-using-jquery) – Evgeny Bovykin Nov 03 '22 at 12:57

1 Answers1

0

Assuming the following data structure:

var result = {
  data: {
    1: 'Sector-5',
    2: 'Sector-20',
    3: 'abc', 
    4: 'Kalka', 
    5: 'Pinjore', 
    6: 'Sector-14', 
    7: 'Sector-7', 
    8: 'MDC', 
    9: 'WPS', 
    10: 'cde', 
    11: 'efg'
  },
  status: "202"
}

Then you can access it as follows.

if (result.status == '202') {
  $.each(result.data, function(key, value){
    console.log(value);
  });
}

See more: https://api.jquery.com/jquery.each/

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • did in same way but got value = [object] which is shown as { 1: 'Sector-5', 2: 'Sector-20', 3: 'abc', 4: 'Kalka', 5: 'Pinjore', 6: 'Sector-14', 7: 'Sector-7', 8: 'MDC', 9: 'WPS', 10: 'cde', 11: 'efg' } – Amit Dhiman Nov 02 '22 at 05:04
  • @AmitDhiman this would suggest that your data structure is not what you stated. Sounds like you have an Object with Objects contained within. Update your question with a proper example of the data being returned from PHP. – Twisty Nov 02 '22 at 05:27