Nice programing

배열 배열을 평면화하는 방법-끝까지 내려 가지는 않음

nicepro 2020. 12. 4. 20:37
반응형

배열 배열을 평면화하는 방법-끝까지 내려 가지는 않음


이것을 변환하는 가장 좋은 방법은 무엇입니까

[[["Club three Team one", 7800], ["Club three Team two", 7801]], [], [["Club four Team one", 7807], ["Club four Team two", 7808]], []]

으로

[["Club three Team one", 7800], ["Club three Team two", 7801], ["Club four Team one", 7807], ["Club four Team two", 7808]]

루비? flatten은 이것을 다음으로 변환합니다.

["Club three Team one", 7303, "Club three Team two", 7304, "Club four Team one", 7310, "Club four Team two", 7311]

http://apidock.com/ruby/Array/flatten 사용flatten(1)

your_array = [[["Club three Team one", 7800], ["Club three Team two", 7801]], [], [["Club four Team one", 7807], ["Club four Team two", 7808]], []]
your_array.flatten(1)
#=> [["Club three Team one", 7800], ["Club three Team two", 7801], ["Club four Team one", 7807], ["Club four Team two", 7808]]

참고 URL : https://stackoverflow.com/questions/5612625/how-to-flatten-an-array-of-arrays-but-not-all-the-way-down

반응형