“Array Salin JavaScript” Kode Jawaban

JavaScript Salin Array

let arr =["a","b","c"];
// ES6 way
const copy = [...arr];

// older method
const copy = Array.from(arr);
Batman

Salin array javascript

const sheeps = ['Apple', 'Banana', 'Juice'];

// Old way
const cloneSheeps = sheeps.slice();

// ES6 way
const cloneSheepsES6 = [...sheeps];
Panzer

Salin array javascript

setMyArray(oldArray => [...oldArray, newElement]);
Inexpensive Impala

Array Salin JavaScript

var numbers = [1,2,3,4,5];
var newNumbers = Object.assign([], numbers);
Beautiful Bear

Array Salin JavaScript

var oldColors=["red","green","blue"];
var newColors = oldColors.slice(); //make a clone/copy of oldColors
Grepper

Array Salin JavaScript

// this is for array with complex object
var countries = [
  {name: 'USA', population: '300M'}, 
  {name: 'China', population: '1.6B'}
];

var newCountries = JSON.parse(JSON.stringify(countries));
Beautiful Bear

Jawaban yang mirip dengan “Array Salin JavaScript”

Pertanyaan yang mirip dengan “Array Salin JavaScript”

Lebih banyak jawaban terkait untuk “Array Salin JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya