+ 1

How can I add new array elements at the beginning of an array in Javascript?

26th Nov 2017, 10:58 PM
Omar Yacoub
Omar Yacoub - avatar
2 Antworten
+ 2
Something like this? var arr = []; arr.unshift(1) arr.unshift(2) arr.unshift(3) console.log(arr) > [3, 2, 1]
26th Nov 2017, 11:08 PM
Mickel
Mickel - avatar
0
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join()); The output of the above code will be something like this, //Output Jani,Hege,Stale,Kai Jim,Borge Jani,Hege,Lene,Stale,Kai Jim,Borge
26th Nov 2017, 11:19 PM
Renjith P
Renjith P - avatar