₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,067 members, 8,420,138 topics. Date: Thursday, 04 June 2026 at 12:03 PM

Toggle theme

Lets Solve Some Javascript Problems - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingLets Solve Some Javascript Problems (1283 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 SolveLets Solve Some Javascript ProblemsJavascript Problems234

[FREE] Anonymity Tools And How To Protect Your Privacy OnlineSimple JS: Jquery Classes HelperGet Premium Access To For Udemy And Skill Share