I'm Drew Town a web developer and systems engineer in Colorado. Always learning, traveling and exploring. Sharing updates, trials and tribulations in tech and life.
A web developer. A guitar / bass / shamisen player. A hobby audio engineer. A world citizen who has broad tastes and passions in various languages (programming / non-programming), music and food.
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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
A couple of common use cases are:
Exactly. I see it is like a preparation for js getting into the two hot topics:
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.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.