Skip to content

Instantly share code, notes, and snippets.

@thomhos
Last active October 17, 2016 12:37
Show Gist options
  • Save thomhos/0c0f9c85f0840b468825f320986b6760 to your computer and use it in GitHub Desktop.
Save thomhos/0c0f9c85f0840b468825f320986b6760 to your computer and use it in GitHub Desktop.
Array functions in ES6
const $buttons = document.querySelectorAll('button');
// Create array from an array-like object
const buttonLabels = Array.from($buttons, button => button.textContent) // ['text', 'text']
// Find something in an array
const arr = [1, 2, 8]
arr.find(x => x > 5) // 8
arr.findIndex(x => x === 2) // 1
// Fill / replace values in an array
const newArr = arr.fill('text', 0, 1); // ['text', 'text', 8]
// Create an array with some values only added conditionally *can also be used for objects the same way
const something = true;
const arr = [1,2,3,...(something?['something']:[]),4];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment