let
name
=
'liu'
;
let
index
=
this
.
userList
.
findIndex
(
item
=>
item
.
name
===
name
)
;
console
.
log
(
index
)
下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~
通过 findIndexlet userList = [ { name: 'liu', age: 18 }, { name: 'li', age: 15 }, { name: 'zhao', age: 14 }];let name = 'liu';let index = this.userList.findIndex(item => item.name === name);console.log(index) // 打印出来的结果是 : 0..
删除
数组
指定的某个
元素
js删除
数组
中
某一项或几项的几种方法 https://www.jb51.net/article/154737.htm
首先可以给JS的
数组
对象
定义一个函数,用于
查找
指定的
元素
在
数组
中
的位置,即
索引
,代码为:
Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
return -1;
然后使用通过得到这个
元素
的
索引
,使用js
数组
自己固有的函数去删除这个
元素
:
Array.prot
//返回
数组
元素
索引
的方法 indexOf(
数组
元素
) 作用就是返回该
数组
元素
的
索引
号.从前面开始
查找
//它只返回第一个满足条件的
索引
号
//它如果在该
数组
里面找不到
元素
,则返回的是-1
var arr = ['apple','banana ','watermelon','grape','banana']
console.log(arr.indexOf('banana'));
//返回
数组
元素
索引
的方法 lastIndexOf(数..
前言: 最近正好写项目用到了这个方法,还是经同事提醒想到的 为此找了相关的知识, 希望可以让看到此博客的人有帮助吧,本人也研究的不深 非喜勿喷 还希望有大神多多指教
正文开始.....
一、findIndex() 方法的定义和使用原理
1.为
数组
中
的每个
元素
都调用一次函数的执行
①. 当
数组
中
的
元素
在需求条件时返回 true
②. 如果没有符合条件的
元素
返回 -1
③. 对于空
数组
,函数不执行
④. 并没有改变
数组
的原始值
public static void main(String[] args) {
int[] arr = new int[]{68, 27, 95, 88, 171, 996, 51, 210};
Scanner sc =new Scanner(System.in);
public class Application {
public static void main(String[] args) {
int arr[]={1,2,54,32,122,3,12,1};
//根据
元素
查找
出该
元素
第一次在
数组
中
出现的
索引
int index= getIInde
在JavaScript
中
,可以使用indexOf()方法来判断
数组
对象
中
是否包含某个
元素
。这个方法会返回要
查找
的
元素
在
数组
中
的
索引
值,如果没找到则返回-1。举个例子,如果我们要判断一个
数组
中
是否包含数字5,可以这样写代码:
var arr = [1, 2, 3, 4, 5];
if (arr.indexOf(5) !== -1) {
console.log('
数组
中
包含数字5');
} else {
console.log('
数组
中
不包含数字5');
这里使用了 !== 操作符,因为indexOf()方法返回的
索引
值可能是0,也就是第一个
元素
的位置,此时如果使用 === 操作符就会被认为是false,而我们需要的是true。
另外,如果我们要判断一个
数组
对象
中
是否包含某个
对象
,可以使用find()方法或findIndex()方法。这两个方法接收一个回调函数作为参数,用于判断每个
元素
是否符合要求。如果找到符合要求的
元素
,则返回该
元素
或
索引
值,否则返回undefined或-1。举个例子,如果我们要判断一个
数组
对象
中
是否包含id为1的
对象
,可以这样写代码:
var arr = [{id: 1, name: 'Tom'}, {id: 2, name: 'Jerry'}, {id: 3, name: 'Mickey'}];
var item = arr.find(function(obj) {
return obj.id === 1;
if (item) {
console.log('
数组
对象
中
包含id为1的
对象
');
} else {
console.log('
数组
对象
中
不包含id为1的
对象
');
这里使用了find()方法,返回找到的符合要求的
对象
,如果没有找到则返回undefined。如果我们要获取符合要求的
对象
的
索引
值,可以使用findIndex()方法替代find()方法。
vue + element + select 获取下拉框的 label 和 value 值( 例如 绑定的是 id , 但后台要求即传 id ,又要传 id 对应的 name )