[英]Destructuring key, value, and index of an object in es6
Can you destructure the key, value, and index of an object in a forEach?你能在 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); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.