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 :: matrix calculator in js 
Javascript :: javascript Access Set Elements 
Javascript :: javascript remaining elements of an array to a variable using the spread syntax 
Javascript :: javascript Read Only View of an Object 
Javascript :: javascript AutoCorrection in Date Object 
Javascript :: Create JavaScript Generators 
Javascript :: JavaScript Code Blocks 
Javascript :: socket io join multiple rooms 
Javascript :: find the missing number javascript 
Javascript :: jQuery - Set 
Javascript :: how to divide a month into weeks in moment js 
Javascript :: timertask jquery 
Javascript :: Javascript: take every nth Element of Array 
Javascript :: change origin xy phaser 
Javascript :: phaser place on circles 
Javascript :: phaser create animation without frame names 
Javascript :: How to call the API when the search value changes 
Javascript :: Six escape sequences are valid in JavaScript 
Javascript :: Expresiones regulares para diferentes tipos de campos de formularios 
Javascript :: getauth firebase admin node.js 
Javascript :: how to used xpath snapshot in loop 
Javascript :: × error: element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. you likely forgot to export your component from the file it 
Javascript :: fibonacci sequence array 
Javascript :: inertia.js 
Javascript :: array objects 
Javascript :: javascript keyboard shortcuts 
Javascript :: Lazy Loading 
Javascript :: how to get country code in react native 
Javascript :: function with .map javascript 
Javascript :: array example 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =