Created
May 16, 2019 04:30
-
-
Save rosek86/ff1982f794d8d2b830d86eea447410f0 to your computer and use it in GitHub Desktop.
JS Array
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
| // concat | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const y = [ 4, 5, 6 ]; | |
| const o = x.concat(y); | |
| console.log(`concat: ${o}`); | |
| })(); | |
| // every | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o = x.every((v) => v < 5); | |
| console.log(`every: ${o}`); | |
| })(); | |
| // some | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o = x.some((v) => v <= 1); | |
| console.log(`some: ${o}`); | |
| })(); | |
| // filter | |
| (() => { | |
| const x = [ 1, 2, 5, 7 ]; | |
| const o = x.filter((v) => v > 4); | |
| console.log(`filter: ${o}`); | |
| })(); | |
| // map | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o = x.map((v) => v * v); | |
| console.log(`map: ${o}`); | |
| })(); | |
| // foreach | |
| (() => { | |
| const data = { entry: [ 'e1', 'e2', 'e3' ] }; | |
| const x = [ 1, 2, 3 ]; | |
| let o = ''; | |
| x.forEach(function (v, i) { | |
| o += `${i}-${v} ${this.entry[i]} `; | |
| }, data); | |
| console.log(`forEach: ${o}`); | |
| })(); | |
| // indexOf | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o1 = x.indexOf(2); | |
| const o2 = x.indexOf(5); | |
| console.log(`indexOf: ${o1}, ${o2}`); | |
| })(); | |
| // lastIndexOf | |
| (() => { | |
| const x = [ 1, 2, 3, 2 ]; | |
| const o1 = x.lastIndexOf(2); | |
| const o2 = x.lastIndexOf(5); | |
| console.log(`lastIndexOf: ${o1}, ${o2}`); | |
| })(); | |
| // join | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o = x.join('-') | |
| console.log(`join: ${o}`); | |
| })(); | |
| // push/pop | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const len1 = x.push(5); | |
| const out1 = [...x]; | |
| const el = x.pop(); | |
| const len2 = x.length; | |
| const out2 = [...x]; | |
| console.log(`push/pop: ${len1}#${out1} | ${el} | ${len2}#${out2}`); | |
| })(); | |
| // unshift/shift | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const len1 = x.unshift(5); | |
| const out1 = Array.from(x); | |
| const el = x.shift(); | |
| const len2 = x.length; | |
| const out2 = x.slice(); | |
| console.log(`unshift/shift: ${len1}#${out1} | ${el} | ${len2}#${out2}`); | |
| })(); | |
| // reduce | |
| // reduceRight | |
| // reverse | |
| (() => { | |
| const x = [ 1, 2, 3 ]; | |
| const o = x.reverse(); | |
| console.log(`reverse: ${o}`); | |
| })(); | |
| // sort | |
| (() => { | |
| // NOTE: in place sort | |
| const x = [ 7, 1, 2, 3 ]; | |
| const out1 = Array.from(x.sort()); | |
| const out2 = Array.from(x.sort((a, b) => b - a)); | |
| console.log(`sort: ${out1} | ${out2}`); | |
| })(); | |
| // slice | |
| // splice | |
| // toString | |
| (() => { | |
| // NOTE: in place sort | |
| const x = [ 7, 1, 2, 3 ]; | |
| const o = x.toString(); | |
| console.log(`toString: |${o}|`); | |
| })(); | |
| // find | |
| // findIndex | |
| // entries | |
| // from | |
| // keys | |
| // [].copyWithin | |
| // [].fill | |
| // [].includes | |
| // [].values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment