js一个数组是另一个数组的下标,根据下标数组查找另一个数组中对应的值,返回新的数组
时间: 2024-04-07 22:33:12 浏览: 195
可以使用 JavaScript 的 map() 方法来实现这个功能。首先,你需要一个下标数组和一个值数组。然后, map() 方法遍历下标数组,在回调中通过下标来获取对应的值,并其返回。最后,得到的新数组是对应的值数组。
下面是一个示例代码:
```javascript
function findValuesByIndices(indices, values) {
return indices.map(index => values[index]);
}
// 示例用法
const indices = [0, 2, 4];
const values = ['a', 'b', 'c', 'd', 'e'];
const result = findValuesByIndices(indices, values);
console.log(result); // 输出: ['a', 'c', 'e']
```
在示例中,`findValuesByIndices()` 函数接受两个参数:`indices` 和 `values`。`indices` 数组表示要查找的下标,`values` 数组是要查找的值。`map()` 方法会遍历 `indices` 数组,并使用每个下标获取对应的值,最终返回一个新的数组。
注意:这里假设下标数组中的值是有效的,并且不会超出值数组的范围。如果存在无效或越界的下标,需要添加相应的错误处理逻辑。
相关问题
js在一个数组对象中查找另一个数组全部元素的下标
在JavaScript中,如果你有一个数组`arrayToFind`,你想在一个较大的数组`mainArray`中查找`arrayToFind`的所有元素对应的下标,你可以使用`Array.prototype.findIndex()`或`Array.prototype.indexOf()`方法结合循环来实现。这里有一个简单的例子:
```javascript
function findIndexes(mainArray, arrayToFind) {
let indexes = [];
for(let i = 0; i < mainArray.length; i++) {
if(mainArray.includes(arrayToFind[i])) {
indexes.push(i);
}
}
return indexes;
}
let mainArray = [1, 2, [3, 4], 5, [6, 7]];
let arrayToFind = [3, 6];
let result = findIndexes(mainArray, arrayToFind);
console.log(result); // 输出: [1, 3],因为在mainArray中,3在索引1处,6在索引3处
```
如果`indexOf`返回-1,说明元素不存在于主数组中,那么`findIndex`会返回-1。注意,这种方法假设`arrayToFind`的所有元素都是唯一的,并且都在`mainArray`中。
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回答案。 用js写
在JavaScript中,你可以使用哈希表(也叫字典或对象)来解决这个问题,这被称为“两数之和”问题。这里是一个简单的解决方案:
```javascript
function twoSum(nums, target) {
// 创建一个空对象作为哈希表
const hash = {};
// 遍历数组
for (let i = 0; i < nums.length; i++) {
// 如果目标值减去当前元素的值已经在哈希表中
if (hash[target - nums[i]]) {
// 返回找到的两个数的下标,注意哈希表中的键是数值,所以加一得到对应数组的索引
return [hash[target - nums[i]] + 1, i + 1];
}
// 如果当前元素不在哈希表中,将它添加进去并设置值为它的索引
hash[nums[i]] = i;
}
// 如果没有找到满足条件的组合,返回null或undefined
return null; // 或者 throw new Error('No solution found.');
}
```
这个函数首先尝试查找目标值与数组中已知元素的差,如果找到这样的差,则说明已经找到了一对数,因为差值的另一个数在之前的位置。如果没有找到,就将当前元素及其索引存入哈希表继续搜索。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)