Last active
August 17, 2020 13:47
-
-
Save zimejin/8efb6714d3a20fdfcec11da7c655211c to your computer and use it in GitHub Desktop.
array utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using slice() to Remove value from beginning and end of string. | |
sliceString = function (value) { | |
let result = value.slice(1, -1); | |
return result; | |
} | |
// Using splice() method to changes the contents of an array by removing | |
// or replacing existing elements and/or adding new elements | |
var months = ['Jan', 'March', 'April', 'June']; | |
spliceGene = function (array) { | |
let result = array.splice(1, 3, 'Feb'); | |
return result; | |
} | |
// unshift: Adds item to front of array | |
unshift = function () { | |
var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
fruits.unshift("Lemon", "Pineapple"); | |
console.log(fruits) | |
} | |
shift = function () { | |
var fruits = ["Banana", "Orange", "Apple", "Mango"]; | |
var x = fruits.shift(); | |
console.log(x); | |
} | |
// We can use the shift() method in while loop like this. | |
shift2 = function () { | |
let names = ["Harvey", "Donna", "Mike", "Rachel", "Louis", "Jessica"]; | |
while ((i = names.shift()) !== undefined) { | |
console.log(i); | |
} | |
} | |
// Ways to Remove Elements From A JavaScript Array | |
var ar = [1, 2, 3, 4, 5, 6]; | |
ar.pop(); // returns 6 | |
console.log(ar); // [1, 2, 3, 4, 5] | |
var ar = ['zero', 'one', 'two', 'three']; | |
ar.shift(); // returns "zero" | |
console.log(ar); // ["one", "two", "three"] | |
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; | |
var removed = arr.splice(2, 2); // removed === [3, 4] | |
["bar", "baz", "foo", "qux"] | |
list.splice(0, 2) | |
// Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"] | |
// /Removing Array Items By Value Using Splice | |
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] === 5) { | |
arr.splice(i, 1); | |
} | |
} | |
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0]; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] === 5) { | |
arr.splice(i, 1); | |
i--; | |
} | |
} | |
// Using the Array filter Method to Remove Items By Value | |
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; | |
var filtered = array.filter(function (value, index, arr) { | |
return value > 5; | |
}); | |
// Making a Remove Method | |
function arrayRemove(arr, value) { | |
return arr.filter(function (ele) { | |
return ele != value; | |
}); | |
} | |
var result = arrayRemove(array, 6); | |
// result = [1, 2, 3, 4, 5, 7, 8, 9, 0] | |
// Explicitly Remove Array Elements Using the Delete Operator | |
var ar = [1, 2, 3, 4, 5, 6]; | |
delete ar[4]; // delete element with index 4 | |
console.log(ar); // [1, 2, 3, 4, undefined, 6] | |
alert(ar); // 1,2,3,4,,6 | |
// Clear or Reset a JavaScript Array | |
var ar = [1, 2, 3, 4, 5, 6]; | |
ar = []; | |
var ar = [1, 2, 3, 4, 5, 6]; | |
console.log(ar); // Output [1, 2, 3, 4, 5, 6] | |
ar.length = 0; | |
console.log(ar); // Output [] | |
var ar = [1, 2, 3, 4, 5, 6]; | |
console.log(ar); // Output [1, 2, 3, 4, 5, 6] | |
ar.splice(0, ar.length); | |
console.log(ar); // Output [] | |
var ar = [1, 2, 3, 4, 5, 6]; | |
console.log(ar); // Output [1, 2, 3, 4, 5, 6] | |
while (ar.length) { | |
ar.pop(); | |
} | |
console.log(ar); // Output [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment