← Back to Ruby Course | Chapter 4: Arrays | Lesson 4 of 7

flatten क्या है

flatten lists के अंदर lists लेता है और इन्हें एक plain list में फैला देता है, छोटे boxes को एक बड़ी tray में खाली करने की तरह। यह extra layers remove करता है।

In this page:

  1. flatten
Syntax
ruby
nested.flatten
nested.flatten(depth)

flatten

flatten arrays के एक nested array को एक single flat array में collapse करता है, default से recursively। एक depth argument pass करना, जैसे flatten(1), limit करता है nesting के कितने levels collapse हों।

यह आमतौर पर map जैसे operations के बाद चाहिए होता है जो arrays-of-arrays produce कर सकते हैं।

Note: flatten(1) सिर्फ एक level deep flatten करता है, useful जब आप कुछ nested structure preserve करना चाहें।

उदाहरण: flatten

markup
nested = [1, [2, 3, [4, 5]], 6]

puts nested.flatten.inspect
puts nested.flatten(1).inspect
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि flatten array बदल देगा, जब यह एक नया return करता है जब तक आप flatten! इस्तेमाल न करें।
  2. यह भूल जाना कि flatten default से पूरी तरह deep जाता है, इसलिए सिर्फ एक level flatten करने के लिए flatten(1) चाहिए।
  3. hashes के एक array को flatten करना और hashes के खुलने की उम्मीद करना, जब सिर्फ arrays flatten होते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.