Skip to content

Instantly share code, notes, and snippets.

@tgrecojs
Created February 4, 2019 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgrecojs/e21efedeef91c418ec46ef6d867c8ae3 to your computer and use it in GitHub Desktop.
Save tgrecojs/e21efedeef91c418ec46ef6d867c8ae3 to your computer and use it in GitHub Desktop.

Intro

1. Show Array

  • Let's pretend for a second that this pplFromAPI array isn't hardcoded, and instead it's being returned to us depending on service calls elsewhere in our application.
  • It's our job to transform this data into a 1dimensional array for the UI to iterate over and display accordingly.

2. - const flattenData = (arr = peopleArray) => [].concat(...ar)

  • One way of implementing this is using .concat in conjunction with the ...spread operator introduced with the release of ES6.

3 "highligh dots"

Shields us having to iterate thru each nested array, building up the final value on each pass.

4. Inspect result

  • We see that var name holds all of our data in one array. In other words, our array has been flattened.
  • Upon checking the requirements however o we realized that several of the endpoints return the string value inside of arrays. --->

5. paste nested array

  • And if we look at the result from render data we see all of these nested arrays are still present.
  • This logic would involve us checking for nested arrays of the array and unbox anź however the
  • .flat method

oour function to work recu to the flat method joining the array.prototype family

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment