Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,180,424 members, 7,911,051 topics. Date: Monday, 05 August 2024 at 12:16 AM

Lets Solve Some Javascript Problems - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Lets Solve Some Javascript Problems (1209 Views)

Check In: Submit Your C++ Questions Lets Solve / Lets Solve Some Javascript Problems / Javascript Problems (2) (3) (4)

(1) (Reply) (Go Down)

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)

Win @cmapit's N100,000 For Developers / Delivering Suitable Ios App Development Solutions / Who Exactly Is A Developer.

(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. 6
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.