DEV Community

Discussion on: Useful features of ECMAScript 2019 reviewed

Collapse
 
drewtownchi profile image
Drew Town

When do people find themselves using/needing .flat()? I don't think I've ever run into a scenarios where I have nested arrays that I need flattened?

I'm curious as to other's experiences when they find they need flat.

Collapse
 
yashints profile image
Yaser Adel Mehraban

A couple of common use cases are:

  1. Image data manipulation
  2. Machine learning data prep
  3. Working 2D or 3D shapes
  4. Searching for an element in a nested array without multiple loops
Collapse
 
schaan profile image
Schaan Alq. • Edited

Exactly. I see it is like a preparation for js getting into the two hot topics:

  1. data science;
  2. VR (computational geometry)
Collapse
 
rudolfolah profile image
Rudolf Olah

If you're dealing with tree structures, like in a nested JSON data object, you may want to select deeply nested items across multiple sub-structures and then use flat() to flatten the array. An example in Ruby is flattening a list of data attributes from a database request. If you're using es2019 on the backend with node.js, flat() could be useful in that case.

Collapse
 
rbauhn profile image
Robin Bauhn

I very rarely use it but I actually did it yesterday. Most often it's when I run multiple queries to GCP's Firestore (because they don't have OR statements...) and get a bunch of arrays with results in an array with all the promises that I await to let them resolve. Then I have a nested array which is quite neat to flatten.