Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,872 members, 7,956,291 topics. Date: Monday, 23 September 2024 at 09:13 AM |
Nairaland Forum / Science/Technology / Programming / Lets Solve Some Javascript Problems (1216 Views)
Check In: Submit Your C++ Questions Lets Solve / Lets Solve Some Javascript Problems / Javascript Problems (2) (3) (4)
Lets Solve Some Javascript Problems by elvongray: 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] 2 Likes
|
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"] |
(1) (Reply)
Complete Video Tutorial For Android Application Development On A Memory Card / Programming Books / Win @cmapit's N100,000 For Developers
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9 |