0

I am using Object.entries because I want to use the key and value but I also want access to the whole item. Is it possible to get the parent of key and value as well?

const object1 = {
  a: 'somestring',
  b: 42
};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
  // How to get the parent of key and value ie [key, value];
}
Miheo
  • 517
  • 9
  • 19
UXCODA
  • 1,118
  • 2
  • 18
  • 33
  • 4
    `[ key, value ]` isn’t a “parent”. If you want `[ key, value ]`, use `[ key, value ]` or just don’t destructure. But why would you need this array? – Sebastian Simon Mar 14 '22 at 02:46
  • Thanks, I didnt actually end up needing it but I was curious how it would work. – UXCODA Mar 14 '22 at 02:49

1 Answers1

1

Destructure in the first line instead?

const object1 = {
  a: 'somestring',
  b: 42
};

for (const entry of Object.entries(object1)) {
  const [key, value] = entry;
  console.log(`${key}: ${value}`);
  console.log(entry);
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320