Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

javascript - 从数组中存在的所有子数组中获取所有元素,而不从主体数组中删除元素? [重复](Get all elements from all subarrays present in an array without removing element from the principal array? [duplicate])

I work on a project with a lot of arrays and in some case I have this:

(我在一个有很多数组的项目上工作,在某些情况下,我有这个问题:)

let main_array = [a,b,c,d,[e,f,g],h,i,j,[k,l,m,o],[p],q,[]]

In my main array I have a lot of subarrays, and I want to get all element from them without removing the other elements from the main array.

(在我的主数组中,我有很多子数组,我想从中获取所有元素,而又不从主数组中删除其他元素。)

So I want a function which can do this:

(所以我想要一个可以做到这一点的函数:)

f(main_array) --> [a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q]

The particularity with the main array is about the number of arrays inside it, in fact, it could have 0 to n subarray(s) with 0 to n element(s).

(主数组的特殊性是关于其内部数组的数量,实际上,它可以具有0到n个子数组以及0到n个元素。)

Is there an existing built-in function or just a simple function with filter for example that can solve this problem in a few line of code (just 1 could be great !) ?

(是否存在现有的内置函数或仅带有过滤器的简单函数,例如,可以在几行代码中解决此问题(仅1个就可以了!)?)

PS: I use node.js

(PS:我使用node.js)

  ask by bosskay972 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...