How to add nesting to an items in array Javascript -


so if want remove nesting array items can it:

var nestedarrs = [[1],[2],[3],[4],[5]]; var denestedarr = [].concat.apply([], nestedarrs); console.log(denestedarr) //prints [1, 2, 3, 4, 5]; 

but if need go backwards , add nesting?

var unnestedarr = [7, 8, 9, 10]; var nestedarr = [] ? // want add nesting every other item in arr console.log(nestedarr); //so wanted print [[7], 8, [9], 10] //& nested arr needs based upon starting unnestedarr. 

var unnestedarr = [7, 8, 9, 10];    var nestedarr = unnestedarr.map(function(item, index) {      return index % 2 === 0 ? [ item ] : item;  });    console.log(nestedarr);


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -