I want to take an array of any length (in this example length of 10):
var fruits = ["Banana", "Orange", "Apple", "Mango", "Strawberry", "Lime", "Kiwi", "Melon", "Pineapple", "Date"];
From this array I want to take elements in increments of 5, convert each increment into a string, then store each string as a nested array within a new array. Each element will need to be seperated by a '%'.
An output like:
newArray = [[ 'Banana%Orange%Apple%Mango%Strawberry' ],[ 'Lime%Kiwi%Melon%Pineapple%Date' ]]
To convert into a string I'm using:
var finalArray = Array()
var x = ""
for(i = 0; i < fruits.length; i++){
if(i==fruits.length-1){
x = x + fruits[i].toString()
}
else {
x = x + fruits[i].toString()+'%'
}
} finalArray.push([x])
Which outputs:
[['Banana%Orange%Apple%Mango%Strawberry%Lime%Kiwi%Melon%Pineapple%Date']]
I've attempted many for & forEach loops, if/else statements etc. in an effort to split the original array into increments of 5 before applying the string conversion code but have not been successful.
Any help or ideas on how to achieve would be appreciated. Thanks.
EDIT: Thanks all, this has answered my question :)