1/* Copying arrays or parts of arrays in JavaScript */
2
3var fruit = ["apple", "banana", "fig"]; // Define initial array.
4console.log(fruit); // ["apple", "banana", "fig"]
5
6// Copy an entire array using .slice()
7var fruit2 = fruit.slice();
8console.log(fruit2); // ["apple", "banana", "fig"]
9
10// Copy only two array indicies rather than all three
11// From index 0 (inclusive) to index 2 (noninclusive)
12var fruit3 = fruit.slice(0,2);
13console.log(fruit3); // ["apple", "banana"]
1/* Copying arrays or parts of arrays in JavaScript */
2
3var fruit = ["apple", "banana", "fig"]; // Define initial array.
4console.log(fruit); // ["apple", "banana", "fig"]
5
6// Copy an entire array using .slice()
7var fruit2 = fruit.slice();
8console.log(fruit2); // ["apple", "banana", "fig"]
9
10// Copy only two array indicies rather than all three
11// From index 0 (inclusive) to index 2 (noninclusive)
12var fruit3 = fruit.slice(0,2);
13console.log(fruit3); // ["apple", "banana"]
14
1// this is for array with complex object
2var countries = [
3 {name: 'USA', population: '300M'},
4 {name: 'China', population: '1.6B'}
5];
6
7var newCountries = JSON.parse(JSON.stringify(countries));
8
1var oldColors=["red","green","blue"];
2var newColors = oldColors.slice(); //make a clone/copy of oldColors