当前位置:网站首页>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
边栏推荐
- 968 edit distance
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- [DIY]如何制作一款個性的收音機
- C # use Oracle stored procedure to obtain result set instance
- 自定义限流注解
- R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
- Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
- Deployment of external server area and dual machine hot standby of firewall Foundation
- Study notes of grain Mall - phase I: Project Introduction
- Reference frame generation based on deep learning
猜你喜欢
面试官:Redis中有序集合的内部实现方式是什么?
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
Common doubts about the introduction of APS by enterprises
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
Common English vocabulary that every programmer must master (recommended Collection)
Redis insert data garbled solution
Performance test process and plan
PHP saves session data to MySQL database
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
随机推荐
##无yum源安装spug监控
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
Solution to the 38th weekly match of acwing
PHP saves session data to MySQL database
2022 nurse (primary) examination questions and new nurse (primary) examination questions
OAI 5G NR+USRP B210安装搭建
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
OSPF多区域配置
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
使用.Net驱动Jetson Nano的OLED显示屏
Reinforcement learning - learning notes 5 | alphago
How to upgrade high value-added links in the textile and clothing industry? APS to help
Reflection operation exercise
967- letter combination of telephone number
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
#yyds干货盘点#重新梳理箭头函数的this
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
R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw