Skip to content
Akin C edited this page Dec 6, 2017 · 17 revisions

Welcome to the Javascript-slicing- wiki!

This repository is part of a larger project!


STILL IN WORK!

In Javascript there exists a method called slice which should enable the coder to select an area of an array content.

The slice method takes either one or two arguments to define in which interval a sample in an array should be taken.

1. Extracting elements with two positive arguments


In this case the first argument should be the beginning and the second should define the ending of the chosen area. The ending argument which indicates to a value in the array is not included in the resulting slice.

var R = [1,2,3,4,5,6,7,8];

//Doesn`t really slice, because beginning and ending arguments are not valid
console.log( R.slice(2,1) );//Outputs:[]
console.log( R.slice(0,0) );//Outputs:[]

//Legit
console.log( R.slice(0,1) );//Outputs:[1]
console.log( R.slice(1,2) );//Outputs:[2]
console.log( R.slice(2,3) );//Outputs:[3]
console.log( R.slice(3,4) );//Outputs:[4]
console.log( R.slice(4,5) );//Outputs:[5]
console.log( R.slice(5,6) );//Outputs:[6]
console.log( R.slice(6,7) );//Outputs:[7]
console.log( R.slice(7,8) );//Outputs:[8]

2. Extracting elments with two negative arguments


The negative value -1 seems the index value of the last element. The ending argument which indicates to a value in the array is not included in the resulting slice.

var R = [1,2,3,4,5,6,7,8];

//Doesn`t really slice, because beginning and ending arguments are not valid
console.log( R.slice(-1,-2) );//Outputs:[]

//Legit
console.log( R.slice(0,-1) );//Outputs:[1, 2, 3, 4, 5, 6, 7]
console.log( R.slice(-2,-1) );//Outputs:[7]
console.log( R.slice(-3,-2) );//Outputs:[6]
console.log( R.slice(-4,-3) );//Outputs:[5]
console.log( R.slice(-5,-4) );//Outputs:[4]
console.log( R.slice(-6,-5) );//Outputs:[3]
console.log( R.slice(-7,-6) );//Outputs:[2]
console.log( R.slice(-8,-7) );//Outputs:[1]

3. Extracting elments with one positive argument


If the ending is not defined, which is the case here, then its default value will be the length of the array.

var R = [1,2,3,4,5,6,7,8];

//Legit
console.log( R.slice(1) );//Outputs:[2, 3, 4, 5, 6, 7, 8]
console.log( R.slice(2) );//Outputs:[3, 4, 5, 6, 7, 8]
console.log( R.slice(3) );//Outputs:[4, 5, 6, 7, 8]
console.log( R.slice(4) );//Outputs:[5, 6, 7, 8]
console.log( R.slice(5) );//Outputs:[6, 7, 8]
console.log( R.slice(6) );//Outputs:[7, 8]
console.log( R.slice(7) );//Outputs:[8]

//Doesn`t really slice, because argument is not valid
console.log( R.slice(0) );//Outputs:[1, 2, 3, 4, 5, 6, 7, 8]
console.log( R.slice(8) );//Outputs:[]

4. Extracting elments with one negative argument


var R = [1,2,3,4,5,6,7,8];

//LEGIT
console.log( R.slice(-1) );//Outputs:[8]
console.log( R.slice(-2) );//Outputs:[7, 8]
console.log( R.slice(-3) );//Outputs:[6, 7, 8]
console.log( R.slice(-4) );//Outputs:[5, 6, 7, 8]
console.log( R.slice(-5) );//Outputs:[4, 5, 6, 7, 8]
console.log( R.slice(-6) );//Outputs:[3, 4, 5, 6, 7, 8]
console.log( R.slice(-7) );//Outputs:[2, 3, 4, 5, 6, 7, 8]

//Doesn`t really slice, because argument is not valid
console.log( R.slice(-8) );//Outputs:[1, 2, 3, 4, 5, 6, 7, 8]
console.log( R.slice(-9) );//Outputs:[1, 2, 3, 4, 5, 6, 7, 8]

5. Extracted objects refer to the same objects


//An array with objects
var R =[ {'Hello': 2}, {'How':4}, {'Are':6}, {'You':8}];

//New Array - Contains: "[{'Hello': 2}, {'How':4}]"
var Reference = R.slice(0, 2);

//Overwriting a new value to the object "{'Hello': 2}"
Reference[0]['Hello'] = 10;

//'R' and 'Reference' refer to the same objects which they have within
console.log(Reference[0]);//Outputs: {'Hello': 10}
console.log(R[0]);        //Outputs: {'Hello': 10}

6. Extracting values to a new array is a copy not a reference


var R = [1,2,3,4,5,6,7,8];

//Contains:[7, 8]
var aCopy = R.slice(-2) ;

//Overwriting value "7"
aCopy[0] = 42;

console.log(aCopy[0]);//Outputs: 42
console.log(R[6]);    //Outputs: 7 
Clone this wiki locally