How to append something to an array?
Solution:
Use the
push()
function to append to an array:// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
Now array is
var arr = [
"Hi",
"Hello",
"Bonjour",
"Hola"
];
// append multiple values to the array
arr.push("Salut", "Hey");
Now array is
var arr = [
"Hi",
"Hello",
"Bonjour",
"Hola",
"Salut",
"Hey"
];
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Will print:
Hi
Hello
Bonjour
Hola
Salut
Hey
Update
If you want to add the items of one array to another array, you can use
Array.concat()
:var arr = [
"apple",
"banana",
"cherry"
];
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Will print
["apple", "banana", "cherry", "dragonfruit", "elderberry", "fig"]
http://stackoverflow.com/questions/351409/how-to-append-something-to-an-array
COMMENTS