₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,176 members, 8,510,524 topics. Date: Friday, 30 October 2026 at 02:40 PM

Toggle theme

Lets Solve Some Javascript Problems - Programming - Nairaland

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

1 Reply (Go Down)

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 huh
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"]
1 Reply

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

Is Delphi Faster Than C# • I Need A Good Programmer/javascript Guru In Abeokuta For Quick Job • I Need A Good Developer For Accounting Application Using Laravel