Skip to content

Instantly share code, notes, and snippets.

@pbzona
Created June 8, 2018 17:57
Show Gist options
  • Save pbzona/e7dc2eba47b68bb2d82a719588dba856 to your computer and use it in GitHub Desktop.
Save pbzona/e7dc2eba47b68bb2d82a719588dba856 to your computer and use it in GitHub Desktop.
array thing
// Say you have an array of "items" that you want to iterate over...
const someArray = [
{
name: 'A',
number: 1
},
{
name: 'B',
number: 2
},
{
name: 'C',
number: 3
}
];
// It doesn't matter that the objects have identical properties...in fact, they should!
for (let i = 0; i < someArray.length; i++) {
console.log(`Name is ${someArray[i].name} and number is ${someArray[i].number}`);
}
// You can do even better with an ES6 class:
class Item {
constructor(name, number) {
this.name = name;
this.number = number;
}
}
// ...this way, you have a consistent structure that you can easily use when adding new data :)
const anotherArray = [
new Item('A', 1),
new Item('B', 2),
new Item('C', 3)
];
// try it both ways, iterate over with the for loop above....the results will be identical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment