Created
March 22, 2018 15:37
-
-
Save 6ui11em/b66fc62a2b4152217ec2b90d9046909e to your computer and use it in GitHub Desktop.
Javascript copy array #javascript #js #es6
This file contains hidden or 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
// slice method | |
var sandwiches = ['turkey', 'tuna', 'chicken salad', 'italian', 'blt', 'grilled cheese']; | |
// ['chicken salad', 'italian', 'blt', 'grilled cheese'] | |
var fewerSandwiches = sandwiches.slice(2); | |
// ['chicken salad', 'italian', 'blt'] | |
var fewerSandwiches2 = sandwiches.slice(2, 4); | |
// ['italian', 'blt', 'grilled cheese'] | |
var sandwichesFromTheEnd = sandwiches.slice(-3); | |
var sandwichesCopy = sandwiches.slice(); | |
// ES6 from method | |
var sandwiches = ['turkey', 'tuna', 'chicken salad', 'italian', 'blt', 'grilled cheese']; | |
// ['turkey', 'tuna', 'chicken salad', 'italian', 'blt', 'grilled cheese'] | |
var sandwichesCopy = Array.from(sandwiches); | |
// (6) ['TURKEY', 'TUNA', 'CHICKEN SALAD', 'ITALIAN', 'BLT', 'GRILLED CHEESE'] | |
var sandwichesUppercase = Array.from(sandwiches, function (sandwich) { | |
return sandwich.toUpperCase(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment