Answers for "add an element to an array"

32

js array add element

array.push(element)
Posted by: Guest on September-23-2019
8

javascript adding an array to an array

let chocholates = ['Mars', 'BarOne', 'Tex'];
let chips = ['Doritos', 'Lays', 'Simba'];
let sweets = ['JellyTots'];

let snacks = [];
snacks.concat(chocholates);
// snacks will now be ['Mars', 'BarOne', 'Tex']
// Similarly if we left the snacks array empty and used the following
snacks.concat(chocolates, chips, sweets);
//This would return the snacks array with all other arrays combined together
// ['Mars', 'BarOne', 'Tex', 'Doritos', 'Lays', 'Simba', 'JellyTots']
Posted by: Guest on March-04-2020
2

js add to array

const myArray = ['hello', 'world'];

// add an element to the end of the array
myArray.push('foo'); // ['hello', 'world', 'foo']

// add an element to the front of the array
myArray.unshift('bar'); // ['bar', 'hello', 'world', 'foo']

// add an element at an index of your choice
// the first value is the index you want to add at
// the second value is how many you want to delete (0 in this case)
// the third value is the value you want to insert
myArray.splice(2, 0, 'there'); // ['bar', 'hello', 'there', 'world', 'foo']
Posted by: Guest on November-29-2020
7

java add element to existing array

//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;
Posted by: Guest on June-20-2020
2

how to add to an array js

var colors = ["blue", "red"];
    colors.push("black")
Posted by: Guest on April-11-2021
2

adding element to array javascript

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
Posted by: Guest on December-02-2019

Code answers related to "add an element to an array"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language