获取过滤数组项的索引

Gre*_*eso 4 javascript arrays indexing filter

在JavaScript中,我有以下数组

var arr = [5, 10, 2, 7];
Run Code Online (Sandbox Code Playgroud)

从该数组中,我想得到一个仅包含小于10的项的索引的数组。因此,在上面的示例中,indexs数组为

var indexes = [0, 2, 3];
Run Code Online (Sandbox Code Playgroud)

现在,我想要类似的东西filter,但这将返回索引。

如果我尝试filter,这就是它的工作方式

var newArr = arr.filter(function (d) {
    return (d < 10);
});

// newArr will be: [5, 2, 7];
Run Code Online (Sandbox Code Playgroud)

这不是我想要的。我想要以下内容(请注意,这是一个伪代码)

var indexes = arr.filter(function (d) {
    /* SOMETHING ALONG THE FOLLOWING PSEUDOCODE */
    /* return Index of filter (d < 10); */
});

// indexes will be: [0, 2, 3];
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?谢谢。

Olu*_*ule 5

使用减速器。

var arr = [5, 10, 2, 7];

var newArr = arr.reduce(function(acc, curr, index) {
  if (curr < 10) {
    acc.push(index);
  }
  return acc;
}, []);


console.log(newArr);
Run Code Online (Sandbox Code Playgroud)