Skip to content

Instantly share code, notes, and snippets.

@6ui11em
Created March 22, 2018 15:37
Show Gist options
  • Save 6ui11em/b66fc62a2b4152217ec2b90d9046909e to your computer and use it in GitHub Desktop.
Save 6ui11em/b66fc62a2b4152217ec2b90d9046909e to your computer and use it in GitHub Desktop.
Javascript copy array #javascript #js #es6
// 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