最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get Indexes of Filtered Array Items - Stack Overflow

programmeradmin2浏览0评论

In JavaScript, I have the following array

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

From that array, I would like to get an array containing only the indexes of the items that are less than 10. So, in the above example, the indexes array would be

var indexes = [0, 2, 3];

Now, I want something simlar to filter, but that would return the indexes.

If I try filter, this is how it will work

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

// newArr will be: [5, 2, 7];

This is not what I want. I want something along the following lines (note this is a pseudo-code)

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

// indexes will be: [0, 2, 3];

How can I do that? Thanks.

In JavaScript, I have the following array

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

From that array, I would like to get an array containing only the indexes of the items that are less than 10. So, in the above example, the indexes array would be

var indexes = [0, 2, 3];

Now, I want something simlar to filter, but that would return the indexes.

If I try filter, this is how it will work

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

// newArr will be: [5, 2, 7];

This is not what I want. I want something along the following lines (note this is a pseudo-code)

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

// indexes will be: [0, 2, 3];

How can I do that? Thanks.

Share Improve this question asked Dec 21, 2017 at 3:32 GreesoGreeso 8,21915 gold badges55 silver badges83 bronze badges 4
  • @Li357 - I tried this. It will not work. Because the map will be indexed to the new filtered array, which will cause shifted indexes. – Greeso Commented Dec 21, 2017 at 3:37
  • What do you mean? – Greeso Commented Dec 21, 2017 at 3:38
  • @Greeso ... filter doesn't mutate the original array. You can still use the original array arr to get the indexes from the filtered array. – ibrahim mahrir Commented Dec 21, 2017 at 3:40
  • 1 Well, what if the original array has items with the same value. Then it will not work. – Greeso Commented Dec 21, 2017 at 3:41
Add a comment  | 

3 Answers 3

Reset to default 11

Use a reducer.

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);

You can use a forEach loop:

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


const customFilter = (arr, min) => {
  const result = [];
  arr.forEach((element, index) => {
    if (element < min) {
      result.push(index);
    }
  });
  
  return result;
}

console.log(customFilter(arr, 10));

You can use array#reduce and add indexes whose value is greater than 10.

var arr = [5, 10, 2, 7];
var indexes = arr.reduce((r, d, i) => d < 10 ? (r.push(i), r) : r , []);
console.log(indexes);

发布评论

评论列表(0)

  1. 暂无评论