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
Post a Comment