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 :: how to signup users without login in firebase js 
Javascript :: mongoose return full object after inserting data to db 
Javascript :: test command in node js 
Javascript :: float vape pen instructions 
Javascript :: Java compile script 
Javascript :: hide show jquery 
Javascript :: merge two array with same length as object 
Javascript :: hello worled anglular script 
Javascript :: three.js first issue resolved awwwards merge webgl html worlds 
Javascript :: stack overflow javascript tree 
Javascript :: form handling in next js 
Javascript :: Vue Apexchart LineChart 
Javascript :: detect paste in textarea 
Javascript :: how to store data in cookie in javascript 
Javascript :: uplaod file in s3 from heroku for node js 
Javascript :: circular objects javascript 
Javascript :: EventEmitter to emit a custom event 
Javascript :: check for changes in other store NUXT JS 
Javascript :: JSX expression with JS template literals 
Javascript :: send data to javscript 
Javascript :: react-native-modal big border 
Javascript :: Last digit of a large number 
Javascript :: react with two components render 
Javascript :: qiankun angular 
Javascript :: Example Of _.extend 
Javascript :: telerik jquery grid trigger editcell 
Javascript :: onSeek video getting paused 
Javascript :: List content on thee currentwdr 
Javascript :: what is container in angular 
Javascript :: javascript chicken 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =