title: retrieving key value pairs from a JSON object - Node JS
body: <p>From the front end I am passing a <code>JSON array</code> to my <code>Node JS</code> backend. Now, I need to retrieve the <code>JSON array</code> and read its <code>key-value</code> pair and print it out in the <code>console</code>. How can I do this. </p>
  
  <p>Code is given as follows:</p>
  
  <p>JQUERY</p>
  
  <pre><code>$( "#send" ).submit(function( event ) {    
    var dict = {};
    $("input[name=name]").each(function (i) {
      dict[$(this).val()] = $('input[name=age]').eq(i).val();
    });
  
    someObject=JSON.stringify(dict, null, 2);
  
   $.ajax({
     url: "food/newpage",
     type: "POST",
     dataType: "json",
     data: { dataobj: someObject },
   });
  });
  </code></pre>
  
  <p>NODE JS</p>
  
  <pre><code>router.post('/newpage', function (req, res, next) {    
    console.log('print ' + req.body.dataobj);
  
    for(var attributename in req.body.dataobj){
      console.log(attributename+": "+req.body.dataobj[attributename]);
    }
  
    res.render('food/newpage');
  });
  </code></pre>
  
  <p>When I printed the <code>req.body.dataobj</code> I get the following output:</p>
  
  <pre><code>{
    "sam": "five",
    "Alex": "fifteen",   
    "Matt": "Two",   
  }
  </code></pre>
  
  <p>My question is: I need to retrieve the JSON array and print it in a <code>console.log</code> as <code>key is equal to the name</code> and <code>value is equal to the age</code>. How can I do this ? </p>
  
  <p><strong>OUTPUT - Update</strong></p>
  
  <blockquote>
    <p>key= 0 and value ={ 0={ key= 1 and value =</p>
    
    <p>1=</p>
    
    <p>key= 2 and value =  2=  key= 3 and value =  3=  key= 4 and value ="
    4=" key= 5 and value =s 5=s key= 6 and value =f 6=f key= 7 and value
    =d 7=d key= 8 and value =s 8=s key= 9 and value =f 9=f key= 10 and value =" 10=" key= 11 and value =: 11=: key= 12 and value =  12=  key=
    13 and value =" 13=" key= 14 and value =a 14=a key= 15 and value =d
    15=d key= 16 and value =f 16=f key= 17 and value =d 17=d key= 18 and
    value =" 18=" key= 19 and value =</p>
    
    <p>19=</p>
    
    <p>key= 20 and value =} 20=}</p>
  </blockquote>