Skip to content

Instantly share code, notes, and snippets.

@TorbjornHoltmon
Last active June 29, 2022 08:48
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 TorbjornHoltmon/cb25b2b2bdbb626e3888a5a562e5489a to your computer and use it in GitHub Desktop.
Save TorbjornHoltmon/cb25b2b2bdbb626e3888a5a562e5489a to your computer and use it in GitHub Desktop.
for each key value loop
const testObject = { a: 1, b: 2, c: 3, d: 4, e: 5 };
// Object.entries({}) lets you loop through the properties of the object with access to the key and value of each entry
for (const [key, value] of Object.entries(testObject)) {
console.log(key, value); // a, 1. b, 2... etc...
}
const customArray = [
{ key: "a", value: 1 },
{ key: "a", value: 1 },
{ key: "b", value: 1 },
{ key: "b", value: 1 },
];
// [].entries() lets you loop through the array with access to the index and value
for (const [index, value] of customArray.entries()) {
console.log(index, value); // 0, { key: "a", value: 1 }. 1, { key: "a", value: 1 }... etc...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment