es6 中对象的解构键、值和索引

BWe*_*303 4 javascript object destructuring ecmascript-6

你能在 forEach 中解构对象的键、值和索引吗?

我理解解构键和值看起来像:

Object.entries(obj).forEach(([key, value]) => {
  ...
});
Run Code Online (Sandbox Code Playgroud)

但我希望也能解构索引。

我的尝试:

Object.entries(obj).forEach((entry, index) => {
    const [key, value] = entry;
    ...
});
Run Code Online (Sandbox Code Playgroud)

但不确定是否有更好的方法。我知道这是一个非常基本的问题,但感谢您的帮助!

Cer*_*nce 10

在解构第一个参数后,只需正常列出索引参数即可:

Object.entries(obj).forEach(([key, value], index) => {
Run Code Online (Sandbox Code Playgroud)

Object.entries(obj).forEach(([key, value], index) => {
Run Code Online (Sandbox Code Playgroud)