javascript - 返回新洗牌元素的索引
问题描述
这是我想要的:
我有first
数组,我有index
如下:
const first = ['a','b','c'];
const index = 1; // the index of b
const shuffled = ['c','a','b']; // now the index of b is 2
在first
数组中index
指向数组中的第二个元素是b
正确的?
现在我打乱了first
数组,我们有了shuffled
数组。
的索引b
现在是 2。我想返回这个新索引...
解决方案
const first = ['a','b','c'];
const index = 1;
const shuffled = ['c','a','b'];
const target = first[index];
const newIndex = shuffled.indexOf(target);
console.log(newIndex);
推荐阅读
- javascript - 使用 jquery 从引导选择选择器文本框中获取值
- python - 在 python 中实现 Sutton 的 One-Step Actor-Critic 伪代码的问题
- encryption - AES-GCM 解密慢 JAVA
- java - Swing setLayout Bug
- php - 如何删除 URL 中的 index.php
- tcp - 进程终止后,WireMock.Net 使端口保持打开状态
- go - 根据我之前是否打印出切片,在附加切片后计算 sha256 会给出不同的结果
- cassandra - CassandraIO:插入日期字段不起作用
- python - 使用 Selenium Python Webdriver 检查我在字段中输入的文本字符串是否正确填充
- amazon-web-services - 创建 lambda 时如何获取 cloudformation 创建的角色