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

copying arrays in javascript without reference | copying arrays using spread operator

let arr1 = [1,2,3,4,5];
let arr2 = [...arr1];
arr2[0] = -2;
console.log(arr2);// outputs : [-2,2,3,4,5]
console.log(arr1);// outputs : [1,2,3,4,5]
Comment

PREVIOUS NEXT
Code Example
Javascript :: ongobd add key value to record 
Javascript :: react-folder tree example 
Javascript :: nav hover add class and remove using javascript smooth 
Javascript :: create user controller 
Javascript :: js 2 varibale points on same values 
Javascript :: get object property dynamically liquid 
Javascript :: laravel data showing in single option instead of multiple option from json array 
Javascript :: javascript Change color based on a keys value in each object of array 
Javascript :: javascript For some reason my content within an array is not printing out to the screen 
Javascript :: how do i set CORS policy for nodejs sever 
Javascript :: Why is the return function in my debounce function never called? Angularjs 
Javascript :: AngularJs: How to interpolate an interpolated string 
Javascript :: show user profile nodejs pug 
Javascript :: Get value from each *ngFor ionic 4, ionic 5, ionic 6 
Javascript :: how to set a condition so that between the first and second mouse clicks there was a delay not 500mls 
Javascript :: javascript unique grouped arrays 
Javascript :: Scaling elements proportionally using CSS and JQUERY 
Javascript :: typeorm-how-to-write-to-different-databases 
Javascript :: How to access POST form fields in Express 
Javascript :: aws amplify react site is blank after updating react-router-dom 
Javascript :: using parseint in javascript 
Javascript :: set of these properties: in js 
Javascript :: phaser change scene 
Javascript :: Sorting the Odd way! 
Javascript :: javascript ls 
Javascript :: how do i block or restrict special characters from input fields with jquery 
Javascript :: prisma count non-null 
Javascript :: NG0100: Expression has changed after it was checked 
Javascript :: Jquery works only on double click 
Javascript :: required field in javascript dynamically 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =