当前位置:网站首页>js判断 数组/对象数组 1 是否包含数组/对象数组 2
js判断 数组/对象数组 1 是否包含数组/对象数组 2
2022-07-29 00:00:00 【闹闹的闹钟】
在开发中我们常用遇到的问题就是查找某个元素是否存在于该数组中,并且多数情况下使用的方法是indexOf(),若元素不存在数组中会返回-1,否则返回元素在数组中的下标。
但是对于对象数组怎么办呢?直接使用indexOf并不能直观的表达出结果。
字符串:
示例:
var str = 'Hello world!';
console.log( str.indexOf('e') ); // 存在字符串中,返回位置 1
console.log( str.indexOf('d') ); // 存在字符串中,返回位置 10
console.log( str.indexOf('x') ); // 不存在字符串中,返回 -1数组:
Array.indexOf() 搜索数组中的元素,并返回它所在的位置,不存在则返回 -1;
Array.includes() 判断一个数组是否包含一个指定的值,返回 true,否则返回false。
Array.find() 返回符合传入测试(函数)条件的数组元素,否则返回 undefined;
Array.findIndex() 返回符合传入测试(函数)条件的数组元素索引,否则返回 -1;
Array.some() 检测数组元素中是否有元素符合指定条件(函数)。返回 true,否则返回 false;
示例:
var arr = ['cat', 'dog', 'bat'];
// indexOf() 方法
console.log( arr.indexOf('cat') ) // 存在 返回位置 0
console.log( arr.indexOf('dog') ) // 存在 返回位置 1
console.log( arr.indexOf('tiger') ) // 不存在 返回 -1
// includes() 方法
console.log( arr.includes('cat' ) ) // 存在 返回 true
console.log( arr.includes('dog' ) ) // 存在 返回 true
console.log( arr.includes('tiger') ) // 不存在 返回 false
// find() 方法
console.log( arr.find(v => v == 'dog' ) ) // 存在 返回值 dog
console.log( arr.find(v => v == 'tiger') ) // 不存在 返回 undefined
// findIndex() 方法
console.log( arr.findIndex(v => v == 'cat' ) ) // 存在 返回 0
console.log( arr.findIndex(v => v == 'dog' ) ) // 存在 返回 1
console.log( arr.findIndex(v => v == 'tiger') ) // 不存在 返回 -1
// some() 方法
console.log( arr.some(v => v == 'cat' ) ) // 存在 返回 true
console.log( arr.some(v => v == 'dog' ) ) // 存在 返回 true
console.log( arr.some(v => v == 'tiger') ) // 不存在 返回 false对象数组:
示例:
var arr = [{name:'cat'}, {name:'dog'}, {name:'bat'}];
var arr2 = [{name:'cat'}, {name:'dog'}]
console.log( arr.find(v => v.name == 'dog' ) ) // 存在 返回 {name: "dog"}
console.log( arr.find(v => v.name == 'tiger') ) // 不存在 返回 undefined
console.log( arr.findIndex(v => v.name == 'cat' ) ) // 存在 返回 0
console.log( arr.findIndex(v => v.name == 'dog' ) ) // 存在 返回 1
console.log( arr.findIndex(v => v.name == 'tiger') ) // 不存在 返回 -1
console.log( arr.some(v => v.name == 'cat' ) ) // 存在 返回 true
console.log( arr.some(v => v.name == 'dog' ) ) // 存在 返回 true
console.log( arr.some(v => v.name == 'tiger') ) // 不存在 返回 false
// 循环判断arr与arr2是否有相同元素,相同的添加isdelete:false字段,不同添加isdelete:true
this.arr.map((item) => {
let bool = this.arr2.findIndex((it) => {
return item.name == it.name
})
if (bool !== -1) {
item.isdelete = false
} else {
item.isdelete = true
}
})
边栏推荐
- ACM SIGIR 2022 | 美团技术团队精选论文解读
- 【unity】将unity编辑c#配置为vscode
- 日期转换 EEE MMM dd HH:mm:ss zzz yyyy
- 【commons-lang3专题】001-StringUtils 专题
- mysql分表之后怎么平滑上线?
- 时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
- FLV文件简介
- How to implement the time impact analysis of the construction project?
- Necessary interview skills for Android (including interview questions and learning materials)
- 小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
猜你喜欢

iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器

Self made | a 16 bit RISC architecture CPU is self-made by hand

Selenium docking agent and selenium wire access developer tool network

线程锁及锁的升降级

NFT 项目的 7 种市场营销策略

Copy the table in word to wechat as a picture and send it

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

How to explain JS' bind simulation implementation to your girlfriend

电子招标初学者指南

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
随机推荐
[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks
量化交易之数字货币篇 - 生成foot print因子数据
如何处理项目中的时间、范围和成本限制?
regular expression
用CDO进行nc数据的不规则裁剪
Self made | a 16 bit RISC architecture CPU is self-made by hand
Summary of process and thread knowledge points 2
B- 树 ~
Solid smart contract tutorial (5) -nft auction contract
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
mysql分表之后怎么平滑上线?
How to create a custom 404 error page in WordPress
正则表达式
Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
Definition of double linked list~
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
可视化全链路日志追踪
【AD学习】本次海上航行器大赛画pcb图的历程
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
Hash table~