1.Array.slice(startIndex,endIndex);

返回由原始数组从startIndex到endIndex-1的元素构成的新数组;

startIndex:默认值0,如果startIndex是负数,则起点从数组的结尾开始,-1表示最后一个元素

endIndex:默认值16777215,如果省略该参数,则片段包括从数组中的startIndex到结尾,如果是负数,则从数组的结尾计数,-1表示数组的最后一个元素

startIndex的位置一定要在endIndex的左边,因为对原始数组的拾取元素是从左往右的

<script >
    var a = [1,2,3,4,5,6,7,8];
    console.log(a.slice(1,4)); //2,3,4
    console.log(a.slice(-2,4)); //空
    console.log(a.slice(1,-3)); //2,3,4,5
    console.log(a.slice(-4,-2)); //5,6
    console.log(a.slice(-4,-5)); //空
</script>

2.将类数组转换成真正的数组

什么是类数组?只要你这个对象里面有length属性,你就可以对外声称自己是类数组。

类数组转换成数组是通过Array.prototype.slice.call(obj)来实现的。

举例:

<script >
    var obj = {
        length:3,
        0:"aaa",
        1:"bbb",
        "name":"pmx"
    }

    var arr = Array.prototype.slice.call(obj)
    console.log(arr);
</script>

这里slice方法解析obj对象,并返回数组arr,arr中的length就是来自于obj中的length属性。slice会扫描obj对象中的属性名能作为索引数组下标的属性,比如这里的0,1。然后将符合的属性作为元素添加到新数组中。

<script >
        var obj = {
            length:5,
            1:"aaa",

            3:"ccc",
            4:"bbb",
            "name":"pmx",
            5:"ddd"

        }

        var arr = Array.prototype.slice.call(obj)
        console.log(arr);
    </script>

数组长度5,将obj对象中属性名小于5的键值对添加到新数组中

由于Array.prototype.slice目的是为了调用slice方法,我们可以[].slice.call(obj),效果一样滴-_-

JS之Array.slice()方法的更多相关文章

  1. js中Array对象方法详解

    操作方法:concat() slice() splice() concat()方法可以基于当前数组中的所有项创建一个新数组.具体来说,这个方法会创建当前数组一个副本,将接收到参数添加到副本的末尾,最后 ...

  2. 兼容低版本JS的Array.map方法

    前几天去别的公司面试遇到个这样的问题,兼容IE7下的Array.map方法,一脸蒙蔽.后面回来查了下资料发现.Array.map方法是ECMA-262 标准中新添加的方法,在低版本的JS中是木有的. ...

  3. JS 中 Array.slice() 和 Array.splice()方法

    slice slice()就是对应String的substring()版本,它截取Array的部分元素,然后返回一个新的Array: var arr = ['A', 'B', 'C', 'D', 'E ...

  4. js中Array数组的属性和方法

    这是我自己整理出来的一些关于Array数组的属性和方法,即查即用. 1.Array.length属性:数组的项数组,始终返回0或者更大的值. 2.instanceof操作符:value instanc ...

  5. JS - Array.slice 与 Array.splice

    1)Array.slice方法   1.1)接收两个参数:              a:起始下标              b:结束下标   1.2)返回由a(包括)至b(不包括)的元素所组成的数组 ...

  6. js原生api之String的slice方法

    我们在工作中可能会很少进行这样的思考,对于一些常用的原生api它是如何实现的呢,如果让我们去用js实现一个与原生api功能相同的函数我们该如何设计算法去实现呢? 为了巩固自己的编程技术和提高自己的编程 ...

  7. JS的splice()方法和slice()方法

    在w3c school中描述如下: 定义和用法splice() 方法用于插入.删除或替换数组的元素.语法arrayObject.splice(index,howmany,element1,....., ...

  8. String方法,js中Array方法,ES5新增Array方法,以及jQuery中Array方法

    相关阅读:https://blog.csdn.net/u013185654/article/details/78498393 相关阅读:https://www.cnblogs.com/huangyin ...

  9. js 字符串截取 substring() 方法、 substr() 方法、slice() 方法、split() 、join();

    三种 js 截取字符串的方法: substring() 方法: substr() 方法: slice() 方法: 1.:substring() 方法:string.substring(from, to ...

随机推荐

  1. LightOJ 1422 (区间DP)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27130 题目大意:按顺序去参加舞会.每个舞会对衣服都有要求.可以 ...

  2. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...

  3. POJ 1724 ROADS(二维SPFA)

    题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include &l ...

  4. COJ967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:C: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  5. winform学习之----将多个控件的click方法绑定到同一click方法中

             public Form3()         {             InitializeComponent();             button1.Click +=new ...

  6. [百科] - CreatePen()

    CreatePen编辑[声明]HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor);[说明]用指定的样式.宽度和颜色创建一个画笔[参数 ...

  7. C# - MVC

    Controllers 位置:Controllers 命名:HomeController   // Views/Home/Index public ActionResult Index() { Vie ...

  8. linphone3.4.0代码分析

    主要类型定义: 1.LinphoneCoreVTable /** * This structure holds all callbacks that the application should im ...

  9. slf4j提示Class path contains multiple SLF4J bindings

    报错: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding .jar!/org/slf4j/impl/St ...

  10. 下载、运行docker

    Get the Linux binary To download the latest version for Linux, use the following URLs: https://get.d ...