javascript - Why is my array returning empty? -
this question has answer here:
- using .slice method on array 1 answer
var array= ["a", "b", "c", "d"] var finalarray= array.slice(2,2); console.log(finalarray);
this returns:
finalarray=[]
i return:
finalarray=["c","d"]
the first argument index @ begin extraction, , second 1 index before end extraction. so, last 2 elements (indexes 2 , 3), need this:
var array= ["a", "b", "c", "d"] var finalarray = array.slice(2, 4); // or slice(2), since 4 length of array console.log(finalarray);
more info on array.prototype.slice()
available in mdn documentation.
Comments
Post a Comment