Last active
October 17, 2016 12:37
-
-
Save thomhos/0c0f9c85f0840b468825f320986b6760 to your computer and use it in GitHub Desktop.
Array functions in 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
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