Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upAlternative of SLICE method. #998
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
ljharb
Sep 11, 2017
Member
Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#feature-requests - this repo is not for language feature requests.
|
Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#feature-requests - this repo is not for language feature requests. |
ljharb
closed this
Sep 11, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tech-Engineer commentedSep 11, 2017
function sliceFunc(origArray, a, b)
{
if(a < 0)
{
a = origArray.length + a;
}
else if(a === undefined)
{
a = 0;
}
if((b<a && b > 0) || b == a)
{
return;
}
else if(b===undefined || b>origArray.length)
{
b=origArray.length;
}
else if(b < 0)
{
b = origArray.length + b;
if(b === a || b < a)
{
return;
}
}
var slicedArray=[];
for(i=a;i<b;i++){
slicedArray.push(origArray[i]);
}
return slicedArray;
}
On frontend we can perform slicing on an array using below syntax:
Present usage of slice method
var numArr = [1, 2, 3, 4, 5];
var subArr = numArr.slice(2, 4);
My proposed way of performing slicing
var numArr = [1, 2, 3, 4, 5];
var subArr = numArr[2...4]; //instead of defining the start and end index by using slice method we can directly define it inside the square bracket.
In my proposed code first number inside square bracket is starting index and second number is end index.