convert array of string to array of object - Javascript Array Operation

Javascript examples for Array Operation:Array of Object

Description

convert array of string to array of object

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from ww w. j a v  a 2 s. c om
var arrOfStrs = ["1","2","3","4","a"];
var arrObj1 = [];
for(var i = 0 ; i < arrOfStrs.length; i++)
{
    arrObj1.push({item: arrOfStrs[i]});
}
console.log(arrObj1);
var arrObj2 = [];
for(var key in arrOfStrs)
{
    arrObj2.push({item: arrOfStrs[key]});
}
console.log(arrObj2);
    }

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

Related Tutorials