admin管理员组文章数量:1331640
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!
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!
Share asked Feb 22, 2020 at 1:05 BWeb303BWeb303 3031 gold badge7 silver badges18 bronze badges1 Answer
Reset to default 10Just 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);
});
本文标签: javascriptDestructuring keyvalueand index of an object in es6Stack Overflow
版权声明:本文标题:javascript - Destructuring key, value, and index of an object in es6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265498a2443313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论