晴明的博客园 GitHub      CodePen      CodeWars     

[js] splice()、slice()

Array.prototype.splice()
array.splice(start, deleteCount[, item1[, item2[, ...]]])

只有当deleteCount为0时,item1、item2...这些参数才有意义
//用新元素替换旧元素,以此修改数组的内容。

//改变原数组

            var myFish = ["angel", "clown", "mandarin", "surgeon"];
            //从第 2 位开始删除 0 个元素,插入 "drum"
            var removed = myFish.splice(2, 0, "drum");
            console.log(myFish);
            //["angel", "clown", "drum", "mandarin", "surgeon"]
            console.log(removed);
            //[]
            
            //从第 3 位开始删除 1 个元素
            removed = myFish.splice(3, 1);
            console.log(myFish);
            //["angel", "clown", "drum", "surgeon"]
            console.log(removed);
            //["mandarin"]
            
            //从第 2 位开始删除 1 个元素,然后插入 "trumpet"
            removed = myFish.splice(2, 1, "trumpet");
            console.log(myFish);
            //["angel", "clown", "trumpet", "surgeon"]
            console.log(removed);
            //["drum"]
            
            //从第 0 位开始删除 2 个元素,然后插入 "parrot", "anemone" 和 "blue"
            removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
            console.log(myFish);
            //["parrot", "anemone", "blue", "trumpet", "surgeon"]
            console.log(removed);
            //["angel", "clown"]
            
            //从第 3 位开始删除 2 个元素
            removed = myFish.splice(3, Number.MAX_VALUE);
            //Number.MAX_VALUE 属性表示在 JavaScript 里所能表示的最大数值。
            //当要删除的值超过后面的长度时,有多少取多少
            console.log(myFish);
            //["parrot", "anemone", "blue"]
            console.log(removed);
            //["trumpet", "surgeon"]
            
            var myFish = ["angel", "clown", "mandarin", "surgeon"];
            removed = myFish.splice(-2, 2);
            //负数则倒序开始
            console.log(myFish);
            //["angel", "clown"]
            console.log(removed);
            //["mandarin", "surgeon"]
            
            removed = myFish.splice(0, -2);
            //负数被识别为0
            console.log(myFish);
            //["angel", "clown"]
            console.log(removed);
            //[]

 

Array.prototype.slice()
array.slice(begin[, end])
//把数组中一部分的浅复制(shallow copy)存入一个新的数组对象中,并返回这个新的数组。
//改变原数组会改变被引用的部分。

            var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
            
            var a = fruits.slice(-3, 4);
            console.log(a);
            //["Lemon"] 负数则从倒序位置开始,end位置不算
            var a = fruits.slice(3, -4);
            console.log(a); //[]
            var a = fruits.slice(4, 1);
            console.log(a); //[] 若start大于 end,则返回[]        
            
            var citrus = fruits.slice(1, 3);
            console.log(fruits);
            //["Banana", "Orange", "Lemon", "Apple", "Mango"]
            console.log(citrus);
            //["Orange","Lemon"]
            var myHonda = {
                color: "red",
                wheels: 4,
                engine: {
                    cylinders: 4,
                    size: 2.2
                }
            };
            var myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
            var newCar = myCar.slice(0, 2);
            
            //console.log("myCar = " + myCar.toSource());
            //toSource(),查看数组的源码,!非标准
            //myCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2, "cherry condition", "purchased 1997"]
            //console.log("newCar = " + newCar.toSource());
            //newCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2]
            console.log("myCar[0].color = " + myCar[0].color);
            //myCar[0].color = red
            console.log("newCar[0].color = " + newCar[0].color);
            //newCar[0].color = red
            myHonda.color = "purple";
            console.log("The new color of my Honda is " + myHonda.color);
            //The new color of my Honda is purple
            console.log("myCar[0].color = " + myCar[0].color);
            //myCar[0].color = purple
            console.log("newCar[0].color = " + newCar[0].color);
            //myCar[0].color = purple
            

 

 

//slice 方法可以用来将一个类数组(Array-like)对象/集合转换成一个数组。
//你只需将该方法绑定到这个对象上。

            function list() {
                return Array.prototype.slice.call(arguments);
                //return [].slice.call(arguments); 另一种写法
            }
            var list1 = list(1, 2, 3); // [1, 2, 3]
            
            //还有另一种写法
            var unboundSlice = Array.prototype.slice;
            var slice = Function.prototype.call.bind(unboundSlice);

            function list() {
                return slice(arguments);
            }
            var list1 = list(1, 2, 3); // [1, 2, 3]

 

posted @ 2016-02-25 02:02  晴明桑  阅读(206)  评论(0编辑  收藏  举报