flatten क्या है
flatten lists के अंदर lists लेता है और इन्हें एक plain list में फैला देता है, छोटे boxes को एक बड़ी tray में खाली करने की तरह। यह extra layers remove करता है।
In this page:
Syntax
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
nested = [1, [2, 3, [4, 5]], 6]
puts nested.flatten.inspect
puts nested.flatten(1).inspect
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- यह उम्मीद करना कि
flattenarray बदल देगा, जब यह एक नया return करता है जब तक आपflatten!इस्तेमाल न करें। - यह भूल जाना कि
flattendefault से पूरी तरह deep जाता है, इसलिए सिर्फ एक level flatten करने के लिएflatten(1)चाहिए। - hashes के एक array को flatten करना और hashes के खुलने की उम्मीद करना, जब सिर्फ arrays flatten होते हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: