Answers for "how to copy one array to another in javascript"

8

copy array javascript

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

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

// ES6 way
const cloneSheepsES6 = [...sheeps];
Posted by: Guest on April-29-2020
5

copy one array to another javascript

/* Copying arrays or parts of arrays in JavaScript */

var fruit = ["apple", "banana", "fig"]; // Define initial array.
console.log(fruit); // ["apple", "banana", "fig"]

// Copy an entire array using .slice()
var fruit2 = fruit.slice(); 
console.log(fruit2); // ["apple", "banana", "fig"]

// Copy only two array indicies rather than all three
// From index 0 (inclusive) to index 2 (noninclusive)
var fruit3 = fruit.slice(0,2); 
console.log(fruit3); // ["apple", "banana"]
Posted by: Guest on April-13-2020
0

js copy array into another

var destinationArray = Array.from(sourceArray);
Posted by: Guest on September-24-2020
3

javascript copy array

var numbers = [1,2,3,4,5];
var newNumbers = Object.assign([], numbers);
Posted by: Guest on April-09-2020
3

javascript copy array

// 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));
Posted by: Guest on April-09-2020
0

how to copy one array to another in javascript

// 1) Array of literal-values (boolean, number, string) 
const type1 = [true, 1, "true"];

// 2) Array of literal-structures (array, object)
const type2 = [[], {}];

// 3) Array of prototype-objects (function)
const type3 = [function () {}, function () {}];
Posted by: Guest on October-28-2020

Code answers related to "how to copy one array to another in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language