Answers for "how to add an element to an array"

48

javascript pushing to an array

some_array = ["John", "Sally"];
some_array.push("Mike");

console.log(some_array); //output will be =>
["John", "Sally", "Mike"]
Posted by: Guest on November-12-2019
1

how to add a new item in an array in javascript

let array = ["Chicago", "Los Angeles", "Calgary", "Seattle", ]
  // print the array 
console.log(array)
  //Adding a new item in an array without touching the actual array     
array.push('New Item') < variable_name > .push( < What you want to add > )
  //How many items does the array have?
console.log("This array has", array.length, "things in it")
Posted by: Guest on March-21-2021
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
2

js add to array

let arr = [1, 2, 3, 4];

arr = [...arr, 5, 6, 7];

console.log(arr);
Posted by: Guest on January-26-2021
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

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

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language