Can you destructure the key, value, and index of an object in a forEach?
I understand destructuring key and value would look like:
Object.entries(obj).forEach(([key, value]) => {
...
});
But I'm hoping to also destructure the index.
My attempt:
Object.entries(obj).forEach((entry, index) => {
const [key, value] = entry;
...
});
But wasn't sure if there was a better way. I know this is a pretty basic question but thanks for the help!
Just list the index argument normally after destructuring the first argument:
Object.entries(obj).forEach(([key, value], index) => {
const obj = {
foo: 'val'
};
Object.entries(obj).forEach(([key, value], index) => {
console.log(key, value, index);
});