[英]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
Parameter 2 (Optional): fromIndex参数2(可选): fromIndex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.