I have a sample array, arr = [5,4,3,3,2,2,1] and i need to generate a multidimensional array like that:
[0] => Array
(
[0] => 5
)
[1] => Array
(
[0] => 4
)
[2] => Array
(
[0] => 3
[1] => 3
)
[3] => Array
(
[0] => 2
[1] => 2
)
[4] => Array
(
[0] => 1
)
I'm not sure about how javascript array works. I've tried to write the code but ended up the all the new array elements is 1
var arr = [5,4,3,3,2,2,1]
var arrtemp = []
var temp = []
var tempval = 0
var col = 0
for (var i=0;i<arr.length;i++){
if (i==0) {
temp.push(arr[i])
tempval = arr[i]
}
else {
if (arr[i] == tempval){
temp.push(arr[i]);
}
else {
arrtemp[col] = temp;
col++;
temp.length =0;
temp.push(arr[i]);
tempval = arr[i];
}
}
}
console.log(arrtemp)