Skip to content

Instantly share code, notes, and snippets.

@himalay
Created August 3, 2017 14:42
Show Gist options
  • Select an option

  • Save himalay/39b2e3a0f23017ad44e2e3f89a6102bd to your computer and use it in GitHub Desktop.

Select an option

Save himalay/39b2e3a0f23017ad44e2e3f89a6102bd to your computer and use it in GitHub Desktop.
var array = [1,2,3,4,5,6]; console.log(array.slice(-1)); // [6] console.log(array.slice(-2)); // [5,6] console.log(array.slice(-3)); // [4,5,6]
var array = [1,2,3,4,5,6]; console.log(array.length); // 6
array.length = 3; console.log(array.length); // 3 console.log(array); // [1,2,3]
Array.push.apply(arr1, arr2) which instead creates a new array – it will merge the second array in the first one reducing the memory usage.
var array1 = [1,2,3]; var array2 = [4,5,6]; console.log(array1.push.apply(array1, array2)); // [1,2,3,4,5,6];
NodeList to Array
var ps=document.querySelectorAll("p");
[].slice.call(ps);
Suffle an array
var list = [1,2,3]; console.log(list.sort(function() { Math.random() - 0.5 })); // [2,1,3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment