diff options
author | name <you@example.org> | 2017-10-31 13:57:42 +0300 |
---|---|---|
committer | name <you@example.org> | 2017-10-31 13:57:42 +0300 |
commit | cbb09c2df6fe9b373457c3b59380038e85f00e92 (patch) | |
tree | 0b839e7079c64d6f43d89b8f684bf1417895b954 /javascript.html.markdown | |
parent | 61c8a084341b006e97ef0ad3f4f66e29db2b8239 (diff) |
[javascript/en] Add several usefull functions to work with arrays
Diffstat (limited to 'javascript.html.markdown')
-rw-r--r-- | javascript.html.markdown | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown index 4ed8f849..75bdf1ad 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -180,6 +180,23 @@ myArray.length; // = 4 // Add/Modify at specific index myArray[3] = "Hello"; +// Add and remove element from front or back end of an array +myArray.unshift(3); // Add as the first element +someVar = myArray.shift(); // Remove first element and return it +myArray.push(3); // Add as the last element +someVar = myArray.pop(); // Remove last element and return it + +// Join all elements of an array with a string +var myArray0 = [32,false,"js",12,56,90]; +myArray0.join(";") // = "32;false;js;12;56;90" + +// Get subarray of elements from index 1 (include) to 4 (exclude) +myArray0.slice(1,4); // = [false,"js",12] + +// Remove 4 elements starting from index 2, and insert there strings +// "33","34" and "35"; return removed subarray +myArray0.splice(2,4,"33","34","35"); + // JavaScript's objects are equivalent to "dictionaries" or "maps" in other // languages: an unordered collection of key-value pairs. var myObj = {key1: "Hello", key2: "World"}; |