Array.prototype.slice()

slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

       const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

        console.log(animals.slice(2));
        // expected output: Array ["camel", "duck", "elephant"]

        console.log(animals.slice(2, 4));
        // expected output: Array ["camel", "duck"]

        console.log(animals.slice(1, 5));
       // expected output: Array ["bison", "camel", "duck", "elephant"]
       console.log(animals) // ['ant', 'bison', 'camel', 'duck', 'elephant']

  

posted @ 2020-03-24 16:56  banzhuxiang  阅读(114)  评论(0)    收藏  举报