How to find index of all occurrences of element in array?

norbdum

I am trying to find the index of all the instances of an element, say, "Nano", in a JavaScript array.

var Cars = ["Nano", "Volvo", "BMW", "Nano", "VW", "Nano"];

I tried jQuery.inArray, or similarly, .indexOf(), but it only gave the index of the last instance of the element, i.e. 5 in this case.

How do I get it for all instances?

nnnnnn

The .indexOf() method has an optional second parameter that specifies the index to start searching from, so you can call it in a loop to find all instances of a particular value:

function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    return indexes;
}

var indexes = getAllIndexes(Cars, "Nano");

You don't really make it clear how you want to use the indexes, so my function returns them as an array (or returns an empty array if the value isn't found), but you could do something else with the individual index values inside the loop.

UPDATE: As per VisioN's comment, a simple for loop would get the same job done more efficiently, and it is easier to understand and therefore easier to maintain:

function getAllIndexes(arr, val) {
    var indexes = [], i;
    for(i = 0; i < arr.length; i++)
        if (arr[i] === val)
            indexes.push(i);
    return indexes;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find index of all occurrences of an element in array (Ramda.js way)?

From Dev

How to find index of all occurrences of an element in array (Ramda.js way)?

From Java

How to find all occurrences of an element in a list

From Dev

How to find index of an Array element in OCaml

From Java

How to find all occurrences of a substring?

From Dev

Find all occurrences (letters) in array (word)

From Dev

When a draw occurs when tracking most occurrences in a list how to find element with highest index?

From Dev

how to find a element in a nested array and get its sub array index

From Dev

How to find and replace all occurrences of a substring in a string?

From Dev

How to Find all occurrences of a Substring in C

From Dev

How to Find all occurrences of a Substring in C

From Dev

How do I find the index of an element in an array, vector or slice?

From Dev

How to use ng-repeat $index to find element in array

From Dev

How to find index of the last non-empty element in a cell array

From Dev

Java: How to find index of last element of partially initialized sorted array

From Dev

How to find the index of last element which is non-zero in array

From Dev

How to find the index of an element in a multidimensional array by JavaScript/JQuery

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

Find the occurrences (count) of all elements at all positions in an array in Postgres?

From Dev

How to count occurrences of an element in a Swift array?

From Dev

find index of array element in another array javascript

From Dev

find index of array element in another array javascript

From Dev

Java - Use binarySearch to find # of occurrences of certain element in array, without If

From Dev

How to find index of a struct element as index of byte array in union (in standard C)

From Dev

Find Occurrences in Array

From Dev

Find Occurrences in Array

From Dev

Find index of element in cell array of matrices

From Dev

How to remove all occurrences of an element from list in Python?

From Dev

How do I find all occurrences of a string in emacs?

Related Related

  1. 1

    How to find index of all occurrences of an element in array (Ramda.js way)?

  2. 2

    How to find index of all occurrences of an element in array (Ramda.js way)?

  3. 3

    How to find all occurrences of an element in a list

  4. 4

    How to find index of an Array element in OCaml

  5. 5

    How to find all occurrences of a substring?

  6. 6

    Find all occurrences (letters) in array (word)

  7. 7

    When a draw occurs when tracking most occurrences in a list how to find element with highest index?

  8. 8

    how to find a element in a nested array and get its sub array index

  9. 9

    How to find and replace all occurrences of a substring in a string?

  10. 10

    How to Find all occurrences of a Substring in C

  11. 11

    How to Find all occurrences of a Substring in C

  12. 12

    How do I find the index of an element in an array, vector or slice?

  13. 13

    How to use ng-repeat $index to find element in array

  14. 14

    How to find index of the last non-empty element in a cell array

  15. 15

    Java: How to find index of last element of partially initialized sorted array

  16. 16

    How to find the index of last element which is non-zero in array

  17. 17

    How to find the index of an element in a multidimensional array by JavaScript/JQuery

  18. 18

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  19. 19

    Find the occurrences (count) of all elements at all positions in an array in Postgres?

  20. 20

    How to count occurrences of an element in a Swift array?

  21. 21

    find index of array element in another array javascript

  22. 22

    find index of array element in another array javascript

  23. 23

    Java - Use binarySearch to find # of occurrences of certain element in array, without If

  24. 24

    How to find index of a struct element as index of byte array in union (in standard C)

  25. 25

    Find Occurrences in Array

  26. 26

    Find Occurrences in Array

  27. 27

    Find index of element in cell array of matrices

  28. 28

    How to remove all occurrences of an element from list in Python?

  29. 29

    How do I find all occurrences of a string in emacs?

HotTag

Archive