1

I have a JSON object and I need to extract a particular array of ids.

This is my jQuery code:

<script>
      jQuery(function() {
          jQuery.getJSON('/api/contents.json', function(data) {
             var lessonContent = data.t;
             console.log(lessonContent);
          });  
      });
</script>

And this is what the Chrome Console shows:

642 {sfwd-lessons: Array(36), sfwd-topic: Array(0), sfwd-quiz: Array(0)}sfwd-lessons: (36) [64601, 64582, 64605, 65065, 65833, 68754, 69691, 73694, 64591, 64693, 65120, 65298, 66005, 72771, 72707, 73416, 74536, 65710, 65712, 65714, 65716, 65718, 65720, 65722, 65724, 65726, 65728, 65730, 65732, 65734, 65736, 65738, 65740, 72028, 72030, 72584]sfwd-quiz: []sfwd-topic: []__proto__: Object

array

How can I access the sfwd-lessons array to get all the listed ids?

Thank you all.

Anonymous
  • 738
  • 4
  • 14
  • 36
hhh
  • 394
  • 2
  • 15

1 Answers1

0

You can use $.each loop and then iterate through data of lessonContent .

Demo Code :

var data = {
  "sfwd-lessons": [64601, 64582, 64605, 65065, 65833, 68754, 69691, 73694, 64591, 64693, 65120, 65298, 66005, 72771, 72707, 73416, 74536, 65710, 65712, 65714, 65716, 65718, 65720, 65722, 65724, 65726, 65728, 65730, 65732, 65734, 65736, 65738, 65740, 72028, 72030, 72584]
}

var lessonContent = data["sfwd-lessons"]
//loop through datas
$.each(lessonContent, function(i, val) {
  console.log(val)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Swati
  • 28,069
  • 4
  • 21
  • 41