0
add variable to array
how to add multiple variables in an array in javascript? ex - var a = abc; var b = xyz ; var c = pqr; var arrdata = new array();
3 ответов
+ 12
var arr = [];
arr.push(x);
……………………
+ 8
or arrdata = [ a, b, c, ... , z... ]
+ 1
There are a number of ways of adding objects to an array.
var array = [0, 2, 4];
// Array.prototype.push: Adds to the end of an array. Can take multiple arguments.
array.push(6); // returns 4 (the new array length)
// Array.prototype.unshift: Adds to the beginning of the array. Can take multiple arguments.
array.unshift(8); // return 5 (the new array length)
// Array.prototype.splice:
// (first argument is the index to add the object, the second is the amount of
// objects to remove starting from the index, and the third is the object to add at the index.
array.splice(5, 0, 10); // returns an array of the removed elements
The last one I have to write is what you can also use to set the array elements. It is in some cases faster than `array.push` (for me, it has always beaten `array.push`). It simply adds an object to the tail of the array.
array[array.length] = 11;