Advertisement
trentjs

get json key titles and values

Nov 16th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <style>
  6.  
  7.     body{
  8.         background-color: #FFFFFF;
  9.         font-family: Arial, Helvetica, sans-serif;
  10.     }
  11.  
  12. </style>
  13.  
  14. </head>
  15. <body>
  16.  
  17. <h2>JsonLoad</h2>
  18.  
  19. <script>
  20.     var jsonSource = [
  21.         {"title-a":"subject-0", "title-b":"thread-0"},
  22.         {"title-a":"subject-1", "title-b":"thread-1"},
  23.         {"title-a":"subject-2", "title-b":"thread-2"},
  24.     ];
  25.  
  26.     var keyList = [];
  27.     var valueList = [];
  28.  
  29.     for (i = 0; i < jsonSource.length; i++) {
  30.         var thisObject = jsonSource[i];
  31.         for(var k in thisObject){
  32.             if(i == 0){
  33.                 keyList.push(k) ;  // push key names
  34.             };
  35.             valueList.push(thisObject[k]); // push key values
  36.         }
  37.     }
  38.  
  39.     console.log("keyList:" + keyList);
  40.     console.log("valueList:" + valueList);
  41.  
  42. </script>
  43.  
  44. </body>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement