jmoreno
0
Q:

how to add to an array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors
79
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); // Fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
9
array = ["hello"]
array.push("world");

console.log(array);
//output =>
["hello", "world"]
15
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
7
array = ["hello"]
array.push("world");
6
//original array
String[] rgb = new String[] {"red", "green"};
//new array with one more length
String[] rgb2 = new String[rgb.length + 1];
//copy the old in the new array
System.arraycopy(rgb, 0, rgb2, 0, rgb.length);
//add element to new array
rgb2[rgb.length] = "blue";
//optional: set old array to new array
rgb = rgb2;
6

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.push("Kiwi");
2
const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
2
const new_array = old_array.concat([value1[, value2[, ...[, valueN]]]])
1
car redCar = new Car("Red");
car Garage [] = new Car [100];
Garage[0] = redCar;
5
var ar = ['one', 'two', 'three'];
ar[3] = 'four'; // add new element to ar
0

New to Communities?

Join the community