Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"]
Comment

javascript slice

//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. 
Comment

slice method javascript

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

const objectArray = [
   {
      name: "Mehedi",
      age: 21
   },
   {
      name: "Ripon",
      age: 25
   },
   {
      name: "Developer",
      age: 22
   },
]

// only start index and it will slice the array from index 2
// upto last element of the array
const sliceStart = array.slice(2)

// start and end index
const sliceStartEnd = array.slice(2, 4)

// negative index
const negativeSlice = array.slice(-2)

// negative end index with positive start index
const negativeSliceStartEnd = array.slice(1, -2)

//slice chaining
const sliceChaining = array.slice(2, 4).slice(0, 4)

// slicing object array
const objectArraySlicing = objectArray.slice(1, 3)

// slicing the first half of the array excluding the middle element
const lengthSlicing = array.slice(Math.floor(array.length / 2), array.length)

// slice then sort in descending order
const sliceSort = array.slice(2, 5).sort((a, b) => b - a)

// slice then filter
const sliceFilter = array.slice(2, 6).filter(i => i > 4)

// slice then map
const sliceMap = array.slice(2, 5).map(i => i * 4)

// returning an array after slicing  
const restParameters = (args) => {
   return args.slice(2, 6)
}

console.log("Slicing with only start index - ", sliceStart)
console.log("Slicing with start and end index - ", sliceStartEnd)
console.log("Slicing with negative index - ", negativeSlice)
console.log("Slicing with negative end index - ", negativeSliceStartEnd)
console.log("Slicing with chaining - ", sliceChaining)
console.log("Slicing with array of objects - ", objectArraySlicing)
console.log("Slicing the second half of the array - ", lengthSlicing)
console.log("Slicing with sort - ", sliceSort)
console.log("Slicing with filter - ", sliceFilter)
console.log("Slicing with map - ", sliceMap)
console.log("Slicing array inside function - ", restParameters(array))
Comment

js slice

const arr=[1,2,3,4,5];

const slicedArr = arr.slice(1,4); // slicedArr = [2,3,4]
Comment

JavaScript array slice

let arrayIntegers = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegers.slice(0, 2); // returns [1,2]
let arrayIntegers2 = arrayIntegers.slice(2, 3); // returns [3]
let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]
Comment

JavaScript slice()

var string = "WelcomeToSofthunt.netTutorialWebsite";
one = string.slice(0, 7)
two = string.slice(7, 9)
three = string.slice(9,21)
four = string.slice(21,29)
five = string.slice(29,36)
six = string.slice(0)

 
document.write(one);
document.write(two);
document.write(three);
document.write(four);
document.write(five);
document.write(six);
Comment

slice javascript

const string="Hi my name is mezen";
string.slice (2); // return string without the character of index 2;
string.slice (6); // return string without the character of index 6;

string.slice (3,7) /* return 'my na' (m of index 3; a of index 7) including the
                      blank spaces */
Comment

slice()

slice(beginIndex)
slice(beginIndex, endIndex)

// beginIndex: The index at which to begin extraction
// endIndex: This index will not be included.
// Return: A new string containing the extracted section.

let str1 = 'The morning is upon us.'
// the length of str1 is 23.
let str2 = str1.slice(1, 8) // OUTPUT: he morn
let str3 = str1.slice(4, -2)// OUTPUT: morning is upon u
let str4 = str1.slice(12) // OUTPUT: is upon us.
let str5 = str1.slice(30) // OUTPUT: ""

// NEGATIVES
beginIndex is negative: 
// it is treated as (str.length + beginIndex)
endIndex is negative: 
// it is treated as (str.length + endIndex)
Comment

js 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"]

console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]

console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]



//SOURCE => MDN web docs
Comment

what is the slice method in javascript

// 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' ]

const numbers = [2, 4, 5, 3, 8, 9, 11, 33, 44];
const sliceNumbers = numbers.slice(2, 5)
console.log(sliceNumbers)
//Expected output:[ 5, 3, 8 ]
Comment

js slice

//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"
Comment

slice in javascript

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
Comment

JavaScript Array slice() example

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let newArray = numbers.slice(4,8);
console.log("The sliced arrays is:", newArray)

// Output: The sliced arrays is: [ 5, 6, 7, 8 ]
Comment

slice js

// slice method 

const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const favFruites=FRUITS.slice(0,1);
const favFruite2=FRUITS.slice(0,3);
const favFruite3=FRUITS.slice(-4);
const favFruite4=FRUITS.slice(-4,-2);


console.log(favFruites);
console.log(favFruite2);
console.log(favFruite3);
console.log(favFruite4);

// output is=

// [ 'Banana' ]
// [ 'Banana', 'Orange', 'Lemon' ]        
// [ 'Orange', 'Lemon', 'Apple', 'Mango' ]
// [ 'Orange', 'Lemon' ]


// note:  its start print value from starting index but not print last indexnumber  


// Returns a copy of a section of an array. For both start and end,
// a negative index can be used to indicate an offset from the end of the array.
//  For example, -2 refers to the second to last element of the array.
Comment

slice in js

//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.
Comment

slice() javascript

let ourString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
ourString.slice(0, 11);
console.log(ourString.slice(0, 11));
//Lorem ipsum

string.slice(start, end);
//Start is required. The position where to begin the extraction. 
//First character is at position 0

//End is optional. 
//The position (up to, but not including) where to end the extraction.
Comment

. slice function javascrpit

const a=[1,2]
//const b=a
//above line makes b point to same object in heap as a

To create a copy of a we can write this instead:
const b=a.slice()
// b now contains it's own object [1,2] in heap

Note:slice method is similar(but a little different) to slice operator in python
For ex. unlike slice operator(python), js slice method only accepts 2 arguments.
Comment

JavaScript slice() Syntax

string.slice(startIndex[, endIndex])
Comment

javascript slice() function


var fruits = new Array ( "apple", "pear", "orange", "banana" );
alert ( fruits.slice ( 1, 3 ) )  // Displays "pear,orange"
alert ( fruits.slice ( 0, -2 ) ) // Displays "apple,pear"
alert ( fruits.slice ( 2 ) )     // Displays "orange,banana"
Comment

PREVIOUS NEXT
Code Example
Javascript :: map function 
Javascript :: typedjs 
Javascript :: es6 import 
Javascript :: npx for yarn 
Javascript :: Using Props With React: With Props 
Javascript :: .default in javascript 
Javascript :: js for loop 
Javascript :: open source 
Javascript :: . is not recognized as an internal command npm run 
Javascript :: angular get firebase firestore 
Javascript :: how to upload document cloddinary 
Javascript :: formatt json to create node and child node react 
Javascript :: sql result to javascript array 
Javascript :: window.location.href breaks back button 
Javascript :: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms 
Javascript :: javqascript sarray push method 
Javascript :: find smallest interval in array javascript 
Javascript :: london turnbridgewells 
Javascript :: regex tester 
Javascript :: excluding a attribute from json strigify 
Javascript :: Insert javascript variable into html string 
Javascript :: discord.js blank field 
Javascript :: scrollable div and unscrollable middle component 
Javascript :: angular absolute routerlink 
Javascript :: model to js 
Javascript :: how to detect if app is loosing focuse in react native 
Javascript :: selenium python grid configuration json 
Javascript :: how to print more than 20 documents mongo shell 
Javascript :: how to turn a page upside down in javascript 
Javascript :: set ibm cloud node environment variables 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =