Convert array into key / value pairs - Javascript jQuery

Javascript examples for jQuery:Array

Description

Convert array into key / value pairs

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
  <script type="text/javascript">
    window.onload=function(){//from   w  w  w.j a v a  2  s.  c  o  m
obj = ["blue", 1, "red", 4, "yellow", 2, "green", 7];
result = [];
for(i=0;i<obj.length;i++){
 if(i%2==0){
  temp = {};
  temp[obj[i]] = obj[i+1];
  result.push(temp);
 }
}
console.log(result);
    }

      </script> 
 </head> 
 <body>  
 </body>
</html>

Related Tutorials