Lets Solve Some Javascript Problems - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Lets Solve Some Javascript Problems (1283 Views)
| Lets Solve Some Javascript Problems by elvongray(op): 5:04pm On Dec 12, 2014 |
Flattening Use the reduce method in combination with the concat method to “flatten” an array of arrays into a single array that has all the elements of the input arrays |
| Re: Lets Solve Some Javascript Problems by losprince(m): 6:43pm On Dec 12, 2014 |
come again ![]() |
| Re: Lets Solve Some Javascript Problems by abdul01(m): 10:12pm On Dec 12, 2014 |
[[1,1,1], [2,2,2], [3,3,3]].reduce(function(a,b){ return a.concat(b)}) Result: [1, 1, 1, 2, 2, 2, 3, 3, 3]
|
| Re: Lets Solve Some Javascript Problems by Cybergenius(m): 12:21pm On Dec 13, 2014 |
//tested and trusted function singleArr(arr){ var ret = [], arr = arr; for(i=0;i<arr.length;i++){ //if the item is an array if(arr[i] instanceof Array){ //loop through and add it to the 'ret' array variable for(c=0;c<arr[i].length;c++) ret.push(arr[i][c]); } else ret.push(arr[i]); } return ret; } console.log( singleArr([0,[1,2,3,4],5,[6,7,8,9],10,"end"]) ); //[0,1,2,3,4,5,6,"end"] |
Check In: Submit Your C++ Questions Lets Solve • Lets Solve Some Javascript Problems • Javascript Problems • 2 • 3 • 4
[FREE] Anonymity Tools And How To Protect Your Privacy Online • Simple JS: Jquery Classes Helper • Get Premium Access To For Udemy And Skill Share
