Member Login:


Remove elements from array by index

Document Reference: TN201808003 - Rev: 4.01 - Last Update: 31-18-2018 10:32 GMT - Downloaded: 24-Jul-2019 08:49 GMT

The mcrSf.arrRemElByIndex() method takes a minimum of two arguments and returns a modified array. The first argument passes a JavaScript Array Object and the second argument passes the index or indexes to be removed. An optional argument can be supplied to switch the return value from the modified array to an array of the removed elements.

Parameters

ParameterTypeDescription
arrObject(Array)Array that elements to be removed from.
elementsNumber, or
String, or
Object(Array)
Index(es) of elements to be removed.
removed
(Optional)
BooleanFlag to return modified array or removed elements.
Optional, default value of false.
cookiePath
(Optional)
StringThe path of the cookie.
Optional, default value of '/'.

Return Value

TypeDescription
Object(Array)Modified array with elements removed, or array of removed elements.

The Stand-Alone Function

 function arrRemElByIndex(arr, elements, removed) {
    removed = removed || false;
    if (typeof elements === "number") {
        var modArr = arr.splice(elements, 1);
    }
    if (typeof elements === "string") {
        var indexes = elements.split("-");
        var modArr = arr.splice(parseInt(indexes[0]),
        (parseInt(indexes[1]) - parseInt(indexes[0]) + 1));
    }
    if (typeof elements === "object") {
        var modArr = [];
        for (var i = elements..length - 1; i > -1 ; i--) {
            modArr.unshift(arr.splice(elements[i], 1)[0]);
        }
    }
    if (removed === false) {
        return arr;
    }
    else {
        return modArr;
    }
}

The mcrSf Library Method

 /**
* removes one or more elements from array by index
* @version 0.1
* @param {Object(Array)} arr - array that elements to be removed from
* @param {Number || String || Object(Array)} elements - index(es) of elements
*     to be removed
* @param {Boolean} [removed=false] - flag to return modified array or removed
*     elements
* @return {Object(Array)} - modified array with elements removed or array of
*     removed elements
*
* mcrSf.arrRemElByIndex(["A", "B", "C", "D"], 2)
* returns ["A", "B", "D"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D"], 2, false)
* returns ["A", "B", "D"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D"], 2, true)
* returns ["C"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], "2-4")
* returns ["A", "B", "F"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], "2-4", false)
* returns ["A", "B", "F"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], "2-4", true)
* returns ["C", "D", "E"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], {1, 3, 4})
* returns ["A", "C", "F"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], {1, 3, 4}, false)
* returns ["A", "C", "F"]
* mcrSf.arrRemElByIndex(["A", "B", "C", "D", "E", "F"], {1, 3, 4}, true)
* returns ["B", "D", "E"]
*
* STRATEGY: Avail of splice method for JavaScript arrays
*/
arrRemElByIndex: function(arr, elements, removed) {
    removed = removed || false;
    if (typeof elements === "number") {
        var modArr = arr.splice(elements, 1);
    }
    if (typeof elements === "string") {
        var indexes = elements.split("-");
        var modArr = arr.splice(parseInt(indexes[0]),
        (parseInt(indexes[1]) - parseInt(indexes[0]) + 1));
    }
    if (typeof elements === "object") {
        var modArr = [];
        for (var i = elements.length - 1; i > -1 ; i--) {
            modArr.unshift(arr.splice(elements[i], 1)[0]);
        }
    }
    if (removed === false) {
        return arr;
    }
    else {
        return modArr;
    }
}

Version History

Revision
Date
Details
0.1
01-Sep-18
First release, arrRemElByIndex().

This website uses cookies to give you the best experience on our website, to personalise content and to analyse our website traffic. Some cookies may have been set already. To find out more about our use of cookies you can visit our Privacy Statement. By browsing this website, you agree to our use of cookies.

Hide this message