Javascript .flat() 不是函数,有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50993498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.flat() is not a function, what's wrong?
提问by TechnoKnight
The following code
以下代码
function steamrollArray(arr) {
// I'm a steamroller, baby
return arr.flat();
}
steamrollArray([1, [2], [3, [[4]]]]);
returns
返回
arr.flatis not a function
arr.flat不是函数
I tried it in Firefoxand Chrome v67and the same result has happened.
我在Firefox和Chrome v67 中尝试过,也发生了同样的结果。
What's wrong?
怎么了?
回答by Ivan
The flatmethod is not yet implementedin common browsers (only Chrome v69, Firefox Nightly and Opera 56). It's an experimental feature. Therefore you cannot use it yet.
该flat方法尚未在常见浏览器中实现(仅 Chrome v69、Firefox Nightly 和 Opera 56)。这是一个实验性功能。因此,你不能用它尚未。
You may want to have your own flatfunction instead:
您可能希望拥有自己的flat功能:
Object.defineProperty(Array.prototype, 'flat', {
value: function(depth = 1) {
return this.reduce(function (flat, toFlatten) {
return flat.concat((Array.isArray(toFlatten) && (depth>1)) ? toFlatten.flat(depth-1) : toFlatten);
}, []);
}
});
console.log(
[1, [2], [3, [[4]]]].flat(2)
);
The code was taken from hereby Noah Freitasoriginally implemented to flatten the array with no depthspecified.
代码是由Noah Freitas从这里获取的,最初实现的目的是在没有指定的情况下展平数组。depth
回答by R.Cha
This can also work.
这也可以工作。
let arr = [ [1,2,3], [2,3,4] ];
console.log([].concat(...arr))
Or for older browsers,
或者对于较旧的浏览器,
[].concat.apply([], arr);
回答by user5513314
Array.flatis not supportedin your browser. Below are two ways to implement it.
Array.flat您的浏览器不支持。下面是实现它的两种方法。
As a function: The depthvariable specifies how deep the inputarray structure should be flattened (defaults to 1; use Infinityto go as deep as it gets) while the stackis the flattened array, passed by reference on recursive calls.
作为函数:depth变量指定input数组结构应该展平的深度(默认为 1;用于Infinity尽可能深入),而stack是展平的数组,在递归调用时通过引用传递。
function flat(input, depth = 1, stack = [])
{
for (let item of input)
{
if (item instanceof Array && depth > 0)
{
flat(item, depth - 1, stack);
}
else {
stack.push(item);
}
}
return stack;
}
Polyfill, extending Array.prototype:
Polyfill,扩展Array.prototype:
if (!Array.prototype.flat)
{
Object.defineProperty(Array.prototype, 'flat',
{
value: function(depth = 1, stack = [])
{
for (let item of this)
{
if (item instanceof Array && depth > 0)
{
item.flat(depth - 1, stack);
}
else {
stack.push(item);
}
}
return stack;
}
});
}
回答by Bash Lord
use _.flatten from lodash package ;)
使用 lodash 包中的 _.flatten ;)
回答by giannis christofakis
Not sure if it is a valid answer however in my attemp to flat an array I employed the destructuring_assignmentintroduced in ES6.
不确定这是否是一个有效的答案,但是在我尝试扁平化数组时,我使用了ES6 中引入的destructuring_assignment。
// typeScriptArray:Array<Object> = new Array<Object>();
let concatArray = [];
let firstArray = [1,2,3];
let secondArray = [2,3,4];
concatArray.push(...firstArray);
concatArray.push(...secondArray);
console.log(concatArray);
It works like a charm even though I'm not sure if any broswer compatibily issues may arise.
尽管我不确定是否会出现任何浏览器兼容问题,但它的作用就像一个魅力。

