Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Copy an Array With the Spread Operator in JavaScript

let studentNames = ["Daniel", "Jane", "Joe"];

let names = [...studentNames];

console.log(names); // ["Daniel","Jane","Joe"]

// This saves us the time we would use to write a loop statement:
Comment

Clone Array Using Spread Operator

let arr1 = [ 1, 2, 3];
let arr2 = arr1;

console.log(arr1); // [1, 2, 3]
console.log(arr2); // [1, 2, 3]

// append an item to the array
arr1.push(4);

console.log(arr1); // [1, 2, 3, 4]
console.log(arr2); // [1, 2, 3, 4]
Comment

copy an array with the spread operator

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

arr2 = [...arr1];

console.log(arr2); // ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
Comment

Copy Array Using Spread Operator

const arr1 = ['one', 'two'];
const arr2 = [...arr1, 'three', 'four', 'five'];

console.log(arr2); 
//  Output:
//  ["one", "two", "three", "four", "five"]
Comment

PREVIOUS NEXT
Code Example
Javascript :: invertir un array javascript 
Javascript :: save array file 
Javascript :: javascript factory functions 
Javascript :: jest write test for function 
Javascript :: sweetalert2 small icon 
Javascript :: why my favicon icon is not removing in react 
Javascript :: angular component 
Javascript :: code to convert rgb to hsl color 
Javascript :: node query selector 
Javascript :: http request body angular 
Javascript :: 2 dimensional array index of element value 
Javascript :: change height of div with scroll in javascript 
Javascript :: mobile detect js 
Javascript :: add two empty arrays javascript 
Javascript :: react footer component 
Javascript :: esql convert blob to json 
Javascript :: React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks 
Javascript :: handling transaction in sequelize 
Javascript :: chart js &php 
Javascript :: populate modal from table 
Javascript :: js callback hell 
Javascript :: vue radio checked if 
Javascript :: JavaScript querySelector - By class 
Javascript :: read dictionary values 
Javascript :: simple todo list javascript 
Javascript :: how i get selected class of li in jquery 
Javascript :: Nuxt.js + Electron 
Javascript :: what is closure 
Javascript :: arcgis for javascript 
Javascript :: run function after another function javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =