Last active
November 17, 2016 21:09
-
-
Save manojsinghnegiwd/cc2bbaf57e0dd6dd80da4ad0f882602c to your computer and use it in GitHub Desktop.
Working with arrays in javascript Part 2
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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; | |
var arrCopy = [110, 120, 130, 140, 150, 160, 170, 180, 190, 200]; | |
var newArr = arr.concat(arrCopy); | |
console.log(newArr); | |
console.log(arr); | |
console.log(arrCopy); | |
// output | |
// [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200] | |
// [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] | |
// [110, 120, 130, 140, 150, 160, 170, 180, 190, 200] |
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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; | |
function divisible (elem) { | |
return elem % 20 == 0 | |
} | |
console.log(arr.filter(divisible)); // output [20, 40, 60, 80, 100] |
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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; | |
function output (elem, index) { | |
console.log(elem + ' is present at ' + (index + 1) + ' position.'); | |
} | |
arr.forEach(output); | |
// outout | |
// 10 is present at 1 position. | |
// 20 is present at 2 position. | |
// 30 is present at 3 position. | |
// 40 is present at 4 position. | |
// 50 is present at 5 position. | |
// 60 is present at 6 position. | |
// 70 is present at 7 position. | |
// 80 is present at 8 position. | |
// 90 is present at 9 position. | |
// 100 is present at 10 position. |
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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; | |
console.log(arr.pop()); // 100 | |
console.log(arr.shift()); // 10 | |
console.log(arr); // [20, 30, 40, 50, 60, 70, 80, 90] |
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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; | |
var revArr = arr.reverse(); | |
console.log(revArr); // output : [100, 90, 80, 70, 60, 50, 40, 30, 20, 10] |
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 str = "This will be converted to an array"; | |
var arr = str.split(" "); | |
console.log(arr); // output ["This", "will", "be", "converted", "to", "an", "array"] | |
var newstr = "This,will,be,converted,to,an,array"; | |
var newarr = newstr.split(","); | |
console.log(newarr); // output ["This", "will", "be", "converted", "to", "an", "array"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment