how to get portion of array in js code example
Example 1: array.slice
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
Example 2: js copy part of array
// The slice function copies parts of an array and
// do therefore not change the default array
let arr = ["apple", "kiwi", "banana", "pear"];
let newArray = arr.slice(1, 2)
console.log(newArray); // output : kiwi