Skip to content

Instantly share code, notes, and snippets.

@bshlgrs
Created June 5, 2017 18:16
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 bshlgrs/5d4d454c8f64114f4ee1fad39ce8d1e9 to your computer and use it in GitHub Desktop.
Save bshlgrs/5d4d454c8f64114f4ee1fad39ce8d1e9 to your computer and use it in GitHub Desktop.

Avoid parallel arrays.

If you want to represent a bunch of people who have names and ages, it’s cleaner to do

let people = [{name: “Buck”, age: 23}, {name: “Daniel”, age: 25}] 

than

let names = [“Buck”, “Daniel”];
let ages = [23, 25];

The most important reason that the former is better is that it's easier to maintain consistency. It is very hard to totally break all your data by inserting a value incorrectly, while it is super easy to break all your data by inserting a value somewhere if you did it the second way.

The other reason that the former is better is that you can pass a person into a function with one function call -- people[x] is all the data about a person.

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