function

splice . array . sncode

VERSION5.87 Splice an array

SPLICE

NAME

splice : Remove elements from an array and replace it with new elements

SYNOPSIS

splice(array, start[, length]);

DESCRIPTION

This function remove elements from an array and replace it with new elements

RETURN

This function returns a new array.

EXAMPLES

input = ["red", "green", ["blue", "yellow"], 1.0, { "x" : 1 }, 4];
input.splice(4, 1, "purple");

return ["red","green",["blue","yellow"],1,"purple",4] which replace the 4th element by the value "purple"

input = ["red", "green", "blue", "yellow"];
input.splice(3, 0, "purple");

return ["red","green","blue","purple","yellow"] which insert an element at position 3.


        input = ["red", "green", "blue", "yellow"];
        splice(input, -1, 1, "black", "maroon");
["red","green","blue","black","maroon"\]


        input = ["red", "green", "blue", "yellow"];
        splice(input, 1, length(input), "orange");
["red","orange"\]


        input = ["red", "green", "blue", "yellow"];
        input.splice(1, -1);
["red","yellow"\]


        input = ["red", "green", "blue", "yellow"];
        input.splice(2);
["red","green"\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(fruits.length()-1, 1, 2, 3, 4);
["Banana","Orange","Apple","Mango",2,3,4\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(0, 5, 1, 2, 3, 4);
[1,2,3,4\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(0, 5);
[\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(-200, -200);
["Banana","Orange","Apple","Mango","Kiwi"\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(200, 200);
["Banana","Orange","Apple","Mango","Kiwi"\]


        fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
        fruits.splice(2, 2);
["Banana","Orange","Kiwi"\]


        fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.splice(2, 1, "Lemon", "Kiwi");
["Banana","Orange","Lemon","Kiwi","Mango"\]


        fruits = ["Banana", "Orange", "Apple", "Mango"];
        fruits.splice(2, 0, "Lemon", "Kiwi");
["Banana","Orange","Lemon","Kiwi","Apple","Mango"\]


        a = [1,2,3,4,5,6];
        x=a.splice(1, 2, 7,8,9);
        a[1]=333;
        a;
        x;
[1,333,3,4,5,6\]\[1,7,8,9,4,5,6\]

 

AUTHOR

Written by Pierre Laplante, <laplante@sednove.com>

VERSION

Available from version 5.90