当前位置: 代码网 > it编程>编程语言>Javascript > JavaScript Array实例方法flat的实现

JavaScript Array实例方法flat的实现

2024年05月18日 Javascript 我要评论
array.prototype.flat()flat()方法用于将一个嵌套多层的数组进行扁平,返回新数组。它不会改变原始数组。 flat 方法在处理多维数组时非常有用,它可以让数组操作变得更加灵活和简

array.prototype.flat()

flat() 方法用于将一个嵌套多层的数组进行扁平,返回新数组。它不会改变原始数组。 flat 方法在处理多维数组时非常有用,它可以让数组操作变得更加灵活和简洁。

语法

flat()
flat(depth)

参数

depth(可选):指定要扁平的深度,默认值为 1。

返回值

一个新的数组,其中包含扁平完的数组元素。

用法

const arr = [0, 1, 2, [3, 4]]
arr.flat()
// [0, 1, 2, 3, 4]

描述

flat() 方法会忽略稀疏数组中的空槽

flat() 方法是个浅拷贝方法,它不会改变原数组。

它只需要 this 值具有 length 属性和整数键属性即可。但是,如果要展开元素,则它们必须是数组

实现 flat 方法

从上面 flat 描述总结实现 flat 时注意实现这三点。

  • 需要一个参数,默认值为 1。
  • flat 会忽略稀疏数组中的空槽。
  • 展开的元素必须是数组。
array.prototype.myflat = function (depth = 1) { // 需要一个参数,默认值为 1
    const result = []

    const flatten = (arr, currentdepth) => {
        for (let i = 0; i < arr.length; i++) {
            if (object.hasown(arr, i)) { // 忽略稀疏数组的空槽
                if (array.isarray(arr[i]) && currentdepth < depth) { // 展开的元素必须是数组
                    flatten(arr[i], ++currentdepth)
                } else {
                    result.push(arr[i])
                }
            }
        }
    }

    flatten(this, 0)
    return result
}

测试用例

array.prototype.myflat = function (depth = 1) {
    const result = []

    const flatten = (arr, currentdepth) => {
        for (let i = 0; i < arr.length; i++) {
            if (object.hasown(arr, i)) {
                if (array.isarray(arr[i]) && currentdepth < depth) {
                    flatten(arr[i], ++currentdepth)
                } else {
                    result.push(arr[i])
                }
            }
        }
    }

    flatten(this, 0)
    return result
}

console.log('扁平普通数组')
const arr1 = [1, 2, [3, 4]]
console.log(arr1.myflat())
console.log(arr1.flat())
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]]
console.log(arr2.myflat())
console.log(arr2.flat())
// [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]]
console.log(arr3.myflat(2))
console.log(arr3.flat(2))
// [1, 2, 3, 4, 5, 6]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]
console.log(arr4.myflat(infinity))
console.log(arr4.flat(infinity))
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


console.log('稀疏数组中使用 flat')
const arr5 = [1, 2, , 4, 5]
console.log(arr5.myflat()) 
console.log(arr5.flat())
// [1, 2, 4, 5]

const arr6 = [1, , 3, ["a", , "c"]]
console.log(arr6.myflat()) 
console.log(arr6.flat())
// [ 1, 3, "a", "c" ]

const arr7 = [1, , 3, ["a", , ["d", , "e"]]]
console.log(arr7.myflat())
console.log(arr7.flat()) 
// [ 1, 3, "a", ["d", empty, "e"] ]

console.log(arr7.myflat(2))
console.log(arr7.flat(2)) 
// [ 1, 3, "a", "d", "e"]


console.log('在非数组对象上使用 flat')
const arraylike = {
    length: 3,
    0: [1, 2],
    1: { length: 2, 0: 3, 1: 4 },
    2: 5,
}
console.log(array.prototype.myflat.call(arraylike))
console.log(array.prototype.flat.call(arraylike))
// [1, 2, { "0": 3, "1": 4, "length": 2 }, 5]

结语

到这里 array 实例方法 flat 实现完成啦。

到此这篇关于javascript array实例方法flat的实现的文章就介绍到这了,更多相关javascript array flat实现内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com