“cara menyalin array javascript” Kode Jawaban

JavaScript Salin Array

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

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

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

cara menyalin array javascript

let arr = ["jason", "james", "jelani"];
let arrCopy = arr.slice();
//Note that this is "slice" and not "splice". 
//arr.splice() would be very different!

console.log(arrCopy);//["jason", "james", "jelani"]

//Makes deep copy
arrCopy[0] = "jamil";
console.log(arr); //["jason", "james", "jelani"]
console.log(arrCopy);//["jamil", "james", "jelani"]
Exuberant Eland

Jawaban yang mirip dengan “cara menyalin array javascript”

Pertanyaan yang mirip dengan “cara menyalin array javascript”

Lebih banyak jawaban terkait untuk “cara menyalin array javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya