获取数组中具有多个位置的值的索引
Getting the index of a value having multiple positions in an array
美好的一天,我目前正在寻找一种方法来查找任何数组中某个值的所有索引。该值可能在该数组中出现多次。我可以使用 .includes
和 .indexOf
来找到第一个位置,比如
function indexOfValue(needle, hayStack) {
if(hayStack.includes(needle)) {
return hayStack.indexOf(needle);
}
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
但这log
只是第一个针位的value
。
这是我试过的全部index
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
if (hayStack.includes(needle)) {
return result.push(hayStack[i]);
}
return result;
}
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
但是上面的代码由于某些原因returns而不是1
[0,5]
。请问这个特定代码有什么问题,我该如何解决?
返回 result.push
会缩短迭代时间,甚至不包括索引。而是检查每个元素是否等于指针,如果相等则压入索引。
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
if (hayStack[i] === needle) { // check if matching
result.push(i); //push the index
}
} return result; //return result at end
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]))
遍历整个数组并为数字的索引创建一个数组。喜欢下面
function indexOfValue(num,arr){
let indexes = [];
for(let i = 0;i<arr.length;i++){
//checks if the element at index 'i' is "num" then push index 'i' into indexes array
if(arr[i] === num) indexes.push(i);
}
return indexes;
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]))
您的代码的问题是您 return 太早了。
每次你从函数中 return
退出函数,停止其中 executing/running 的其余代码。
因此,当您在 for 循环中 return
时,您正在停止循环进行任何其他检查。这意味着您应该 return 在循环完成后。
此外,您还需要修复 for 循环中的 if
语句。目前您正在检查您传入的数组 (hayStack
) 是否包含您要查找的项目 (needle
)。相反,您需要检查当前项目(使用 haystack[i]
)是否为 needle
,如果是,您需要将 i
(即当前索引)推入您的 result
数组。
请参阅下面的工作示例:
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) { // loop through the array, where `i` is the current index in the array
if (hayStack[i] === needle) { // check if a given number inthe array is the `needle`, if it is:
result.push(i); // add the index of the item to the result array
}
}
return result; // only once the loop is complete return the array
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
如果你愿意,你也可以使用高阶函数来完成同样的任务,比如reduce
:
const indexOfValue = (n, arr, i) =>
arr.reduce((acc, num, i) => num === n ? [...acc, i] : acc, [])
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
for
循环中问题 return
的两个代码示例。第二个示例 .push()
是 result
数组的元素,而不是 index
。
您可以使用.indexOf()
的第二个参数来设置开始搜索的索引,检查.indexOf()
的结果是否大于-1
并且索引不在result
数组,return
result
数组在 for
循环后
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
let index = hayStack.indexOf(needle, i);
if (index > -1 && result.indexOf(index) === -1) {
result.push(index);
}
}
return result;
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
const numbers = [11, 3, 6, 8, 11];
const indexOfValue = (val,numbers) =>{
let filtered =[];
numbers.filter((number,index) => {
if(val === number)
filtered = [...filtered,index];
})
return filtered;
}
console.log(indexOfValue(11,numbers));
美好的一天,我目前正在寻找一种方法来查找任何数组中某个值的所有索引。该值可能在该数组中出现多次。我可以使用 .includes
和 .indexOf
来找到第一个位置,比如
function indexOfValue(needle, hayStack) {
if(hayStack.includes(needle)) {
return hayStack.indexOf(needle);
}
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
但这log
只是第一个针位的value
。
这是我试过的全部index
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
if (hayStack.includes(needle)) {
return result.push(hayStack[i]);
}
return result;
}
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
但是上面的代码由于某些原因returns而不是1
[0,5]
。请问这个特定代码有什么问题,我该如何解决?
返回 result.push
会缩短迭代时间,甚至不包括索引。而是检查每个元素是否等于指针,如果相等则压入索引。
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
if (hayStack[i] === needle) { // check if matching
result.push(i); //push the index
}
} return result; //return result at end
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]))
遍历整个数组并为数字的索引创建一个数组。喜欢下面
function indexOfValue(num,arr){
let indexes = [];
for(let i = 0;i<arr.length;i++){
//checks if the element at index 'i' is "num" then push index 'i' into indexes array
if(arr[i] === num) indexes.push(i);
}
return indexes;
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]))
您的代码的问题是您 return 太早了。
每次你从函数中 return
退出函数,停止其中 executing/running 的其余代码。
因此,当您在 for 循环中 return
时,您正在停止循环进行任何其他检查。这意味着您应该 return 在循环完成后。
此外,您还需要修复 for 循环中的 if
语句。目前您正在检查您传入的数组 (hayStack
) 是否包含您要查找的项目 (needle
)。相反,您需要检查当前项目(使用 haystack[i]
)是否为 needle
,如果是,您需要将 i
(即当前索引)推入您的 result
数组。
请参阅下面的工作示例:
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) { // loop through the array, where `i` is the current index in the array
if (hayStack[i] === needle) { // check if a given number inthe array is the `needle`, if it is:
result.push(i); // add the index of the item to the result array
}
}
return result; // only once the loop is complete return the array
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
如果你愿意,你也可以使用高阶函数来完成同样的任务,比如reduce
:
const indexOfValue = (n, arr, i) =>
arr.reduce((acc, num, i) => num === n ? [...acc, i] : acc, [])
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
for
循环中问题 return
的两个代码示例。第二个示例 .push()
是 result
数组的元素,而不是 index
。
您可以使用.indexOf()
的第二个参数来设置开始搜索的索引,检查.indexOf()
的结果是否大于-1
并且索引不在result
数组,return
result
数组在 for
循环后
function indexOfValue(needle, hayStack) {
let result = [];
for(let i = 0; i < hayStack.length; i++) {
let index = hayStack.indexOf(needle, i);
if (index > -1 && result.indexOf(index) === -1) {
result.push(index);
}
}
return result;
}
console.log(indexOfValue(12, [12, 1, 3, 3, 6, 12]));
const numbers = [11, 3, 6, 8, 11];
const indexOfValue = (val,numbers) =>{
let filtered =[];
numbers.filter((number,index) => {
if(val === number)
filtered = [...filtered,index];
})
return filtered;
}
console.log(indexOfValue(11,numbers));