Home > Web Front-end > JS Tutorial > body text

Return value types of Array.slice() and Array.splice()_Basic knowledge

WBOY
Release: 2016-05-16 19:25:22
Original
1392 people have browsed it

I did an exercise recently and used Array.slice() and Array.splice(). I didn’t pay attention to the return value type before.
Array.slice() returns an Array -- an array consisting of a certain range of elements in the original array. array.
Array.splice() returns Array -- an array containing the elements removed from the original array.
What is returned is an array. Use typeof() to see that it is object. This is correct! What you need to remind you is:
When the array elements are numbers, use Array.slice() and Array.splice() To compare the size of one of the elements, please note that the
type must be converted. This is very hidden and it is difficult to check after an error. The
object type cannot be directly converted to number using Number(). You need to String first. ()/8@$
For example:

var arr = [0, 1, 2];
var arr1 = [1, 2, 3];
for (var i = 0; i trace (arr.slice (i,i 1) "--" typeof (arr.slice (i,i 1)));
//output- -0--object.....
trace (arr1.slice (i,i 1) "--" typeof (arr1.slice (i,i 1)));
//output-- 1--object....
trace (arr.slice (i,i 1)//output--false,,,,object is not comparable Size
trace (arr.slice (i,i 1)>arr1.slice (i,i 1));
//output--false,,,,object is not comparable size
trace (Number (arr.slice (i,i 1))//output--undefined,,,,object is not available Number() conversion type
trace (Number(arr.slice (i,i 1).toString())//output--true, first toString( ) and then Number() can
}

trace (arr.slice (i,i 1)trace (arr.slice (i ,i 1)>arr1.slice (i,i 1));
is written as
trace (arr.slice (i,i 1)[0]trace (arr.slice (i,i 1)[0]>arr1.slice (i,i 1)[0]);
Also

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!