Skip to content

Instantly share code, notes, and snippets.

@krfong916
Created October 10, 2018 06:18
Show Gist options
  • Save krfong916/cfde12eaa8a0efd1a46c2dd19df44359 to your computer and use it in GitHub Desktop.
Save krfong916/cfde12eaa8a0efd1a46c2dd19df44359 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/murobul
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function test() {
var arrayBefore = [2, 4, 56, 78, 79, 80, 86, 88, 89, 100, 120, 133, 1234, 1567, 3444, 4567];
// get array length
console.log(arrayBefore.length);
// get value of array at index[length -5]
console.log(arrayBefore[arrayBefore.length - 5]);
// create new array from 0 to 5
console.log(arrayBefore.slice(0, 5));
var arrayAfter = [2, 3, 4, 5];
// merge arrayBefore[] with arrayAfter[], arrayAfter[] is at the end
console.log(arrayBefore.concat(arrayAfter));
// merge arrayBefore from index [0 to 2] with arrayBefore from index [10 to 14]
console.log(arrayBefore.slice(0, 2).concat(arrayBefore.slice(10, 14)));
// using the spread operator, return the array of [0 to 2] and [10, end]
return [].concat(_toConsumableArray(arrayBefore.slice(0, 2)), _toConsumableArray(arrayBefore.slice(10)));
}
console.log('test: ' + test());
</script>
<script id="jsbin-source-javascript" type="text/javascript">function test() {
let arrayBefore = [2, 4, 56, 78, 79, 80, 86, 88, 89, 100, 120, 133, 1234, 1567, 3444, 4567];
// get array length
console.log(arrayBefore.length);
// get value of array at index[length -5]
console.log(arrayBefore[arrayBefore.length-5]);
// create new array from 0 to 5
console.log(arrayBefore.slice(0, 5));
let arrayAfter = [2,3,4,5];
// merge arrayBefore[] with arrayAfter[], arrayAfter[] is at the end
console.log(arrayBefore.concat(arrayAfter));
// merge arrayBefore from index [0 to 2] with arrayBefore from index [10 to 14]
console.log(arrayBefore.slice(0, 2).concat(arrayBefore.slice(10,14)))
// using the spread operator, return the array of [0 to 2] and [10, end]
return [
...arrayBefore.slice(0,2),
...arrayBefore.slice(10)
]
}
console.log('test: ' + test());</script></body>
</html>
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function test() {
var arrayBefore = [2, 4, 56, 78, 79, 80, 86, 88, 89, 100, 120, 133, 1234, 1567, 3444, 4567];
// get array length
console.log(arrayBefore.length);
// get value of array at index[length -5]
console.log(arrayBefore[arrayBefore.length - 5]);
// create new array from 0 to 5
console.log(arrayBefore.slice(0, 5));
var arrayAfter = [2, 3, 4, 5];
// merge arrayBefore[] with arrayAfter[], arrayAfter[] is at the end
console.log(arrayBefore.concat(arrayAfter));
// merge arrayBefore from index [0 to 2] with arrayBefore from index [10 to 14]
console.log(arrayBefore.slice(0, 2).concat(arrayBefore.slice(10, 14)));
// using the spread operator, return the array of [0 to 2] and [10, end]
return [].concat(_toConsumableArray(arrayBefore.slice(0, 2)), _toConsumableArray(arrayBefore.slice(10)));
}
console.log('test: ' + test());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment