简体   繁体   English

如何在同一个数组中找到多个元素的索引

[英]how find the index of multiple elements in the same array

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?为什么它只记录'get'的值,而不是'get'和'easy'的值,我怎样才能返回两者?

Appreciate the help, I am a beginner, sorry if the formatting is bad.感谢帮助,我是初学者,如果格式不好,请见谅。

You can map indexOf over the list of values whose indices are wanted:您可以将indexOf映射到需要索引的值列表上:

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

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. .indexOf()函数语法如下indexOf(searchElement, fromIndex)因此在您的情况下,这意味着它从数组中的“轻松”开始搜索“get”。

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

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. indexOf是一种JavaScript 数组方法,一次只能查找一个元素,无论在数组中搜索什么值。

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.一个更灵活的数组方法来返回你正在寻找的索引是reduce ,它可以让你在迭代后返回你想要的任何值,包括另一个数组,其中的索引值与你的搜索匹配。

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]

According to the indexOf() MDN Web Docs,根据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." “indexOf() 方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。”

The optional use of the second parameter is to set a starting point for that search:第二个参数的可选用途是为该搜索设置一个起点:

Example: indexOf(searchElement, fromIndex)示例: indexOf(searchElement, fromIndex)

Parameter 1: searchElement参数一: searchElement

  • Element to locate in the array.要在数组中定位的元素。

Parameter 2 (Optional): fromIndex参数2(可选): 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.如果索引大于或等于数组的长度,则返回-1,表示不会搜索该数组。 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.如果提供的索引为 0,则将搜索整个数组。 Default: 0 (entire array is searched).默认值:0(搜索整个数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多次查找数组中相同元素的索引 - Find index of same elements in array multiple times 如何在多维数组中将多个元素插入到同一个索引中? - How to insert multiple elements into the same index in a multidimensional array? 如何使用第二个数组元素查找数组中多个元素的索引,然后使用结果匹配第三个数组的索引(Javascript) - How to find indexes of multiple elements in array with second array elements and then use result to match index of third array (Javascript) 如何在索引不连续的数组中按索引同时删除多个元素? - How to delete multiple elements by index at same time in array where indexes are not continuous? 如何将数组数组中相同索引处的元素加到一个数组中? - How to sum elements at the same index in array of arrays into a single array? 如何获得具有两个相同元素的数组中的索引元素? - How to get index element in array with two the same elements? 如何查找数组是否包含具有相同元素的其他数组 - How to find if array include an other array with same elements 如何找到一个数组的多个索引值来调用另一个数组,javascript - how to find multiple index values of an array to call on another array, javascript 如何按索引从数组中删除多个元素? - How do I remove multiple elements from an array by index? 在嵌套数组中查找多个元素 - Find multiple elements in nested array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM