Last active
November 14, 2016 15:18
-
-
Save manojsinghnegiwd/a0f95b9233b774cec08d05e2d44fff6f to your computer and use it in GitHub Desktop.
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
var arr = [ | |
"Apple", | |
"Orange", | |
"Pineapple", | |
"Mango", | |
"Mango", | |
"Pear" | |
]; | |
console.log(arr.indexOf("Orange")) // outputs 1 | |
console.log(arr.indexOf("Watermelon")) // outputs -1 | |
console.log(arr.indexOf("Mango")) // outputs 3 |
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
var arr = [ | |
"1", | |
"2", | |
"3", | |
"4", | |
"5" | |
]; | |
var str = arr.join(","); | |
console.log(str); //outputs 1,2,3,4,5 |
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
var arr = [ | |
"Apple", | |
"Orange", | |
"Pineapple", | |
"Mango", | |
"Mango", | |
"Pear" | |
]; | |
console.log(arr.lastIndexOf("Mango")) // outputs 4 |
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
var arr = [ | |
"1", | |
"2", | |
"3", | |
"4", | |
"5" | |
]; | |
function strToNum (str, index, arr) { | |
console.log(index); | |
console.log(arr); | |
return parseInt(str); | |
} | |
var newArr = arr.map(strToNum); | |
console.log(newArr); // [1, 2, 3, 4, 5] |
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
var arr = [ | |
"1", | |
"2", | |
"3", | |
"4", | |
"5" | |
]; | |
function strToNum (str) { | |
return parseInt(str); | |
} | |
var newArr = arr.map(strToNum); | |
console.log(newArr); // [1, 2, 3, 4, 5] |
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
var arr = [ | |
"1", | |
"2", | |
"3", | |
"4", | |
"5" | |
]; | |
arr.push("6"); | |
console.log(arr); // ["1", "2", "3", "4", "5", "6"] |
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
var arr = [ | |
"Apple", | |
"Orange", | |
"Pineapple", | |
"Mango", | |
"Pear" | |
]; | |
var begin = 1; | |
var end = 2; | |
console.log(arr.slice(begin)); | |
console.log(arr.slice(begin, end)); |
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
var arr = [ | |
"Apple", | |
"Orange", | |
"Pineapple", | |
"Mango", | |
"Pear" | |
]; | |
var index = 0; | |
var elems = 0; | |
var newElem = 'Banana'; | |
console.log(arr.splice(index, elems, newElem)) // add elements to starting of an array | |
// outputs ["Banana", "Apple", "Orange", "Pineapple", "Mango", "Pear"] |
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
var index = 1; | |
var elems = 2; | |
var newElem = 'Banana'; | |
console.log(arr.splice(index, elems)) // add elements to starting of an array | |
console.log(arr) // ["Apple", "Mango", "Pear"] | |
// console.log(arr.splice(index, elems, 'Banana')) you can also add and remove elements from a given index at the same time. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment