当前位置:网站首页>js通过数组内容来获取数组下标
js通过数组内容来获取数组下标
2022-07-06 12:51:00 【viceen】
js通过数组内容来获取数组下标
需求:通过已知数组里面的某个内容,然后要通过这个内容来删除这个数组,是通过循环来把数组的下标找到,然后再拿去删除。
方法1:js写法
var arr = [1, 1, 2, 12, 3, 23, 5, 6, 45]; //数组
var k = 12;//要查找数组的内容
var c = [];//通过内容来获取的数组的下标
//获取下标
for (var i = 0; i < arr.length; i++) {
if (k == arr[i]) {
c.push(i)
}
}
console.log(c) // [3]
方法2:ES6的map语法,
虽然代码简介,但是只能查找第一个,有的地方就是需要第一个就够了,如果有重复的话,就可以采用第一种方法。
var arr = [23,45,312,3213,53,23]
var index = arr.map(item => item).indexOf(23)
console.log(index) // 0
边栏推荐
- Reference frame generation based on deep learning
- OSPF multi zone configuration
- Interviewer: what is the internal implementation of ordered collection in redis?
- OneNote 深度评测:使用资源、插件、模版
- 什么是RDB和AOF
- Spiral square PTA
- 2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
- MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
- User defined current limiting annotation
- C # use Oracle stored procedure to obtain result set instance
猜你喜欢
【mysql】游标的基本使用
OneNote 深度评测:使用资源、插件、模版
[DSP] [Part 1] start DSP learning
知识图谱构建流程步骤详解
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
【论文解读】用于白内障分级/分类的机器学习技术
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Entity alignment two of knowledge map
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
随机推荐
Solution to the 38th weekly match of acwing
Dynamically switch data sources
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Deployment of external server area and dual machine hot standby of firewall Foundation
c#使用oracle存储过程获取结果集实例
防火墙基础之外网服务器区部署和双机热备
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
如何实现常见框架
Swagger UI tutorial API document artifact
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
Xcode6 error: "no matching provisioning profiles found for application"
What are RDB and AOF
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
1_ Introduction to go language
Value of APS application in food industry
Distributed ID
面试官:Redis中有序集合的内部实现方式是什么?
@PathVariable
Yyds dry goods count re comb this of arrow function
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)