node add to array code example

Example 1: javascript append to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors

Example 2: nodejs add element to array

var array = [];
array.push(element)
console.log(array);

Example 3: javascript add items to array

//adding items to array
objects = [];
objects.push("you can add a string,number,boolean,etc");
//if you more stuff in a array
//before i made a error doing value = : value
objects.push({variable1 : value, variable2 : value);
//we do the {} so we tell it it will have more stuff and the variable : value

Example 4: javascript push all elements of array to another array

var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);

Example 5: js add to array

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

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

console.log(arr);

Example 6: add item to array javascript

const arr1 = [1,2,3]
const newValue = 4
const newData = [...arr1, obj] // [1,2,3,4]