当前位置:网站首页>JS judge whether array / object array 1 contains array / object array 2
JS judge whether array / object array 1 contains array / object array 2
2022-07-29 01:20:00 【Alarm clock】
The problem we often encounter in development is to find out whether an element exists in the array , And the method used in most cases is indexOf(), If the element does not exist, the array will return -1, Otherwise, return the subscript of the element in the array .
But what about object arrays ? Use it directly indexOf It can't express the result intuitively .
character string :
Example :
var str = 'Hello world!';
console.log( str.indexOf('e') ); // Exists in the string , Return to position 1
console.log( str.indexOf('d') ); // Exists in the string , Return to position 10
console.log( str.indexOf('x') ); // Does not exist in string , return -1Array :
Array.indexOf() Search for elements in the array , And return to where it is , Returns if it does not exist -1;
Array.includes() Determine whether an array contains a specified value , return true, Otherwise return to false.
Array.find() Return compliance with incoming test ( function ) Conditional array elements , Otherwise return to undefined;
Array.findIndex() Return compliance with incoming test ( function ) Conditional array element index , Otherwise return to -1;
Array.some() Check whether any element in the array element meets the specified conditions ( function ). return true, Otherwise return to false;
Example :
var arr = ['cat', 'dog', 'bat'];
// indexOf() Method
console.log( arr.indexOf('cat') ) // There is Return to position 0
console.log( arr.indexOf('dog') ) // There is Return to position 1
console.log( arr.indexOf('tiger') ) // non-existent return -1
// includes() Method
console.log( arr.includes('cat' ) ) // There is return true
console.log( arr.includes('dog' ) ) // There is return true
console.log( arr.includes('tiger') ) // non-existent return false
// find() Method
console.log( arr.find(v => v == 'dog' ) ) // There is Return value dog
console.log( arr.find(v => v == 'tiger') ) // non-existent return undefined
// findIndex() Method
console.log( arr.findIndex(v => v == 'cat' ) ) // There is return 0
console.log( arr.findIndex(v => v == 'dog' ) ) // There is return 1
console.log( arr.findIndex(v => v == 'tiger') ) // non-existent return -1
// some() Method
console.log( arr.some(v => v == 'cat' ) ) // There is return true
console.log( arr.some(v => v == 'dog' ) ) // There is return true
console.log( arr.some(v => v == 'tiger') ) // non-existent return falseAn array of objects :
Example :
var arr = [{name:'cat'}, {name:'dog'}, {name:'bat'}];
var arr2 = [{name:'cat'}, {name:'dog'}]
console.log( arr.find(v => v.name == 'dog' ) ) // There is return {name: "dog"}
console.log( arr.find(v => v.name == 'tiger') ) // non-existent return undefined
console.log( arr.findIndex(v => v.name == 'cat' ) ) // There is return 0
console.log( arr.findIndex(v => v.name == 'dog' ) ) // There is return 1
console.log( arr.findIndex(v => v.name == 'tiger') ) // non-existent return -1
console.log( arr.some(v => v.name == 'cat' ) ) // There is return true
console.log( arr.some(v => v.name == 'dog' ) ) // There is return true
console.log( arr.some(v => v.name == 'tiger') ) // non-existent return false
// Circular judgement arr And arr2 Whether there are the same elements , Same addition isdelete:false Field , Different addition 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
}
})
边栏推荐
- Summary of process and thread knowledge points 2
- Univariate function integration 1__ Indefinite integral
- Naver 三方登录
- 正则表达式
- Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
- 【mysql】多指标历史累计去重问题
- 【Leetcode-滑动窗口问题】
- How to deal with the time, scope and cost constraints in the project?
- RHCE命令练习(二)
- (perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
猜你喜欢

Oozie工作调度

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

Bracket matching test

括号匹配的检验

QT静态编译程序(Mingw编译)

可视化全链路日志追踪
![[idea] where to use the query field](/img/63/f95868907364fc949885c67c34ba32.png)
[idea] where to use the query field

Wechat campus bathroom reservation applet graduation design finished product (5) assignment

时间复杂度、空间复杂度的学习总结

Spark 3.0 中七个必须知道的 SQL 性能优化
随机推荐
Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
MySQL time is formatted by hour_ MySQL time format, MySQL statement queried by time period [easy to understand]
一元函数积分学之1__不定积分
nep 2022 cat
大页内存原理及使用设置
Solid smart contract tutorial (5) -nft auction contract
mysql分表之后怎么平滑上线?
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
How to implement the time impact analysis of the construction project?
新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
SDRAM Controller Design (two design methods of digital controller)
Linux Redis 源码安装
[target detection] Introduction to yolor theory + practical test visdrone data set
[Commons lang3 topic] 001 stringutils topic
Synchronized关键字详解
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
Naver three party login
Naver 三方登录