Last active
June 29, 2022 08:48
-
-
Save TorbjornHoltmon/cb25b2b2bdbb626e3888a5a562e5489a to your computer and use it in GitHub Desktop.
for each key value loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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