Last active
April 3, 2016 06:21
-
-
Save chuck0523/dc40f6073b74f2f1f6da8d8f45c7ff5e to your computer and use it in GitHub Desktop.
This file contains 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
import flatten from 'array-flatten' | |
/* | |
配列の平坦化 | |
*/ | |
let array = [1, 2, [3, 4, [5, 6]], 7] | |
console.log(array) | |
// [1, 2, [3, 4, [5, 6]], 7] | |
console.log(flatten(array)) | |
// [ 1, 2, 3, 4, 5, 6, 7 ] | |
console.log(flatten.depth(array, 1)) | |
// [ 1, 2, 3, 4, [ 5, 6 ], 7 ] | |
/* | |
配列ライクな構造の平坦化 | |
*/ | |
let obj = { | |
1: 'one', | |
2: 'two', | |
3: { '1st': 'first', '2nd': 'second' }, | |
4: [1, 2, 3, [4.1, 4.2, 4.3]] | |
} | |
console.log(obj) | |
// { '1': 'one', | |
// '2': 'two', | |
// '3': { '1st': 'first', '2nd': 'second' }, | |
// '4': [ 1, 2, 3, [ 4.1, 4.2, 4.3 ] ] } | |
let flatArrayLike = (...args) => flatten.from(args) | |
console.log(flatArrayLike(1, [2, 3, [4, 5]])) | |
// [ 1, 2, 3, 4, 5 ] | |
let flatArrayLikeDepth = (...args) => flatten.fromDepth(args, 1) | |
console.log(flatArrayLikeDepth(1, [2, 3, [4, 5]])) | |
// [ 1, 2, 3, [ 4, 5 ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment