Array.push() appends one or more elements to the end of an array and modifies the original array, returning the new length. Use array.push(...otherArray) to add all elements from another array.
Spread operator (...) creates a new array by merging two arrays without modifying the originals.
let array1: number[] = [1, 2];
let array2: number[] = [3, 4];
let mergedArray: number[] = [...array1, ...array2]; // [1, 2, 3, 4]Array.concat() returns a new array by combining the original array with one or more arrays or values.
let mergedArray = array1.concat(array2); // [1, 2, 3, 4]Use push(...otherArray) for in-place modification, spread operator or concat() for creating new arrays.
Use the concat function, like so:
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).
Use the concat function, like so:
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).
In ECMAScript 6, you can use the Spread syntax:
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);
console.log(arr1)
Spread syntax is available in all major browsers (that excludes IE11). For the current compatibility, see this (continuously updated) compatibility table.
However, see Jack Giffin's reply below for more comments on performance. It seems concat is still better and faster than the spread operator.
Hi there!
I'm new to JavaScript and was learning about arrays today and saw that the instructor only added an array to another array. Is it possible to add something that is not an array into an array? Iyes, how can I do that?
Thanks in advance!
Three ways to append an item to an array (Mutative)
How to append something to an array?
How to push an object into an array with Typescript - Stack Overflow
Can you only add an array to another array?
Use the Array.prototype.push method to append values to the end of an array:
Copy// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can use the push() function to append more than one value to an array in a single call:
Copy// initialize array
var arr = ["Hi", "Hello", "Bonjour", "Hola"];
// append multiple values to the array
arr.push("Salut", "Hey");
// display all values
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Note that the push() method returns the updated length of the array.
Update
If you want to add the items of one array to another array, you can use firstArray.concat(secondArray):
Copyvar arr = [
"apple",
"banana",
"cherry"
];
// Do not forget to assign the result as, unlike push, concat does not change the existing array
arr = arr.concat([
"dragonfruit",
"elderberry",
"fig"
]);
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Update
Just an addition to this answer if you want to prepend any value to the start of an array (i.e. first index) then you can use Array.prototype.unshift for this purpose.
Copyvar arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
It also supports appending multiple values at once just like push.
Update
Another way with ES6 syntax is to return a new array with the spread syntax. This leaves the original array unchanged, but returns a new array with new items appended or prepended, compliant with the spirit of functional programming.
Copyconst arr1 = [
"Hi",
"Hello",
"Bonjour",
];
const arr2 = [
"Ciao",
"Hej",
"Merhaba",
];
const newArr1 = [
...arr1,
"Salut",
];
const newArr2 = [
"Salut",
...arr2,
];
const newArr3 = [
...arr1,
...arr2,
];
console.log(newArr1, newArr2, newArr3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you're only appending a single variable, then push() works just fine. If you need to append another array, use concat():
Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
var ar3 = ar1.concat(ar2);
alert(ar1);
alert(ar2);
alert(ar3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The concat does not affect ar1 and ar2 unless reassigned, for example:
Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];
ar1 = ar1.concat(ar2);
alert(ar1);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
There is a lot of great information on JavaScript Reference.
let myArray = [];
let commentData = {} as Dish;
commentData.id = 3;
commentData.name = 'something';
myArray.push(commentData);
It will work...
#Answer
Answer on how to push Comment (object) into Dish.comments (array) in TypeScript.
export interface Dish {
id: number;
name: string;
image: string;
category: string;
label: string;
price: string;
featured: boolean;
description: string;
// comments: Comment[]; // remove this
comments: Array<Comment>; // <--- change to this. everytime you want to add array use Array<YourInterface>
}
export interface Comment {
rating: number;
comment: string;
author: string;
date: string;
}
dish.comments.push(commentData);
View live code on TypeScript Playground and click RUN.
As you see in the above code. You need to change Comment[] into Array<Comment>.
#Explanation
Generic Type Variables
Array<T> or Array<Type>
You may already be familiar with this style of type from other languages such as Java and C#.
We have Generic Type Variables in typescript too.
Another way to use Generic Type Variables:
Here's an example of an array with multiple types:
let x: Array<string | number>
x = ["hello", "world", 2]
This second version is common if your array consists of different types of objects. For example:
interface Boat {
name: string
}
interface SpaceShip {
code: number
}
interface Wagon {
active: boolean
}
let inventory: Array<Boat | SpaceShip | Wagon> = [];
let boatData: Boat = {
name: "Boat 1"
}
let spaceShipData: SpaceShip = {
code: 1234
}
let wagonData: Wagon = {
active: true
}
inventory.push(boatData);
inventory.push(spaceShipData);
inventory.push(wagonData);
console.log(inventory);
View live code on TypeScript Playground and click RUN.
You can learn more about Generic Type Variables here and here