Monday, February 13, 2017

JavaScript: Add and remove from arrays / arrays as collections




Did you know you can easily add and remove elements from Javascript arrays ?

Declare an array:

var arr = [];



To add an element:

arr.push(item);



To remove an element at index idx:

arr.splice(idx, 1);



To remove an element elm:

var idx = arr.indexOf(elm);

if (idx > -1) arr.splice(idx, 1);



The splice function allows adding and removing items from an array by providing a range (start index and length) and replacement values.

No comments:

Post a Comment

Sql Server: Manage Access and Roles with Transact-SQL (T-SQL) Commands

From: https://technet.microsoft.com/en-us/library/dd433159.aspx Tip: Manage Access and Roles with Transact-SQL (T-SQL) Commands SQL Serv...