Kamran
0
Q:

js slice

// array.slice(start, end)
const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = FRUITS.slice(1, 3);
// citrus => [ 'Orange', 'Lemon' ]

// Negative values slice in the opposite direction
var fromTheEnd = FRUITS.slice(-3, -1);
// fromTheEnd => [ 'Lemon', 'Apple' ]
24
/"slice() copies or extracts a given number of elements to a new array"/

let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];

let todaysWeather = weatherConditions.slice(1, 3);
// todaysWeather equals ['snow', 'sleet'];
// weatherConditions still equals ['rain', 'snow', 'sleet', 'hail', 'clear']
5
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
// ["Orange", "Lemon"]
6
const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"
5
//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.

// same in array but you select elements not characters  


const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"

console.log(str.slice(0, 2)); 
// expected output: "the"
// Up to and including the last index!!!
// Different for python. 
16

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

var citrus = fruits.slice(1, 3);
 
3
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"]
7
//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.

// same in array but you select elements not characters  


const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"
7
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
// citrus value now is:  ["Orange","Lemon"]
3
array.slice(0, n);
1
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var arr1 = arr.slice(3, 5);			// [4, 5]var arr2 = arr.slice(undefined, 5);		// [1, 2, 3, 4, 5]var arr3 = arr.slice(-3);			// [8, 9, 10]var arr4 = arr.slice(-3, 9);			// [8, 9]var arr5 = arr.slice(10);			// []var arr6 = arr.slice(4);			// [5, 6, 7, 8, 9, 10]var arr7 = arr.slice(undefined);		// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]var arr8 = arr.slice(5, -4);			// [6]var arr9 = arr.slice(2, 15);			// [3, 4, 5, 6, 7, 8, 9, 10] console.log(arr);	// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]console.log(arr1);	// [4, 5]console.log(arr2);	// [1, 2, 3, 4, 5]console.log(arr3);	// [8, 9, 10]console.log(arr4);	// [8, 9]console.log(arr5);	// []console.log(arr6);	// [5, 6, 7, 8, 9, 10]console.log(arr7);	// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]console.log(arr8);	// [6]console.log(arr9);	// [3, 4, 5, 6, 7, 8, 9, 10]
-1

New to Communities?

Join the community