diff options
author | Pratik Karki <predatoramigo@gmail.com> | 2017-11-13 18:04:12 +0545 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-13 18:04:12 +0545 |
commit | f924aa690a26de90b2e92381ad0bdd61e9aef268 (patch) | |
tree | bca234a066d419a9b6f088a59e6acb8a68f813de | |
parent | 6ce71c56d6affb57a3537a2732485a4918306d4b (diff) | |
parent | aff623e234d8d0844a44ff5331d756ae539aa6d0 (diff) |
Merge pull request #2992 from Neinei0k/master
[javascript/en] Add several usefull functions to work with arrays
-rw-r--r-- | javascript.html.markdown | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown index 4ed8f849..199c3c09 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -180,6 +180,24 @@ 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 semicolon +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 +// "hi","wr" and "ld"; return removed subarray +myArray0.splice(2,4,"hi","wr","ld"); // = ["js",12,56,90] +// myArray0 === [32,false,"hi","wr","ld"] + // 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"}; |