Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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