角度分割列表 [英] Angular split list
问题描述
我有一个看起来像这样的列表
I have a list looking like so
[[{A},{B}], {C}]
是否有任何简单的方法,而无需创建一个将其展平为如下所示的数组的函数
Is there any simple method without having to create a function that will flatten this to an array looking like so
[{A},{B},{C}]
我搜索了SO,但是使用打字稿方法却找不到任何东西.
I searched on SO but did not find anything with typescript methods.
谢谢.
A,B和C是相同的类型.
Edit : A, B and C are the same types.
推荐答案
It appears that what you're looking for is the built-in Array.prototype.flat
:
const input = [['foo', 'bar'], 'baz'];
const output = input.flat(); // ['foo', 'bar', 'baz']
请注意,您必须在esnext
支持下进行编译.有关更多详细信息,请参见此问题.它也不会在IE或Edge上开箱即用.如果您的用例存在问题,则需要使用上方MDN链接中显示的polyfill.
Note that you'll have to compile this with esnext
support. See this question for more details. It also won't work out of the box on IE or Edge. If that's a problem in your use case, you'll want to use the polyfill that's shown in the MDN link above.
替代方法存在于众多库中,例如, lodash , Ramda 都具有等效的flatten
方法.
Alternatives exist in numerous libaries, for example, lodash, underscore.js, and Ramda all have an equivalent flatten
method.
这篇关于角度分割列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!