Home > Blockchain >  how find the index of multiple elements in the same array
how find the index of multiple elements in the same array

Time:07-13

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
console.log(secretMessage.indexOf('get','easily')

Why does it only log the value of the 'get', not of both 'get' and 'easily', and how can I return both?

Appreciate the help, I am a beginner, sorry if the formatting is bad.

CodePudding user response:

You can map indexOf over the list of values whose indices are wanted:

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

console.log(['get', 'easily'].map(x => secretMessage.indexOf(x)));

CodePudding user response:

The .indexOf() function syntax is as follows indexOf(searchElement, fromIndex) so what that means in your case is its searching for "get" starting from "easily" in the array.

To search both you can try this

console.log(secretMessage.indexOf('get'));
console.log(secretMessage.indexOf('easily'));

and if you need them together you can assign each to a variable then display them together later something like this

let one = secretMessage.indexOf('get');
let two = secretMessage.indexOf('easily');

let result = one   ","   two 

console.log(result);

CodePudding user response:

indexOf is a JavaScript array method that only can look for one element at a time, whatever value is being searched for in the array.

A more flexible array method for returning the indices of what you're looking for would be reduce, which lets you return whatever value you want after iterating, including another array with the indices of the values that match your search.

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

const matchingIndices = secretMessage.reduce((results, currentValue, currentIndex)=> {
  if (currentValue === 'get' || currentValue === 'easily'){
    results.push(currentIndex)
  }

  return results;
}, [])

console.log(matchingIndices) // => [6, 7]

CodePudding user response:

According to the indexOf() MDN Web Docs,

"The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present."

The optional use of the second parameter is to set a starting point for that search:

Example: indexOf(searchElement, fromIndex)

Parameter 1: searchElement

  • Element to locate in the array.

Parameter 2 (Optional): fromIndex

  • The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the provided index is 0, then the whole array will be searched. Default: 0 (entire array is searched).
  • Related