当前位置:网站首页>js实现查看一个数组对象中是否包含另一个数组对象中的值
js实现查看一个数组对象中是否包含另一个数组对象中的值
2022-06-24 06:46:00 【张小浪】
//两个数组对象中查看 其中一个数组对象是否包含另一个数组对象中的值
let arr = [
{key:1,val:"张胜男"},
{key:2,val:"王五"},
{key:3,val:"李四"},
{key:4,val:"刘超"},
{key:5,val:"赵四"}
]
let arr2 = [
{key:1,val:"张胜男"},
{key:2,val:"王五"},
{key:6,val:"666"},
{key:7,val:"777"},
{key:8,val:"888"},
{key:9,val:"999"},
]
//如果能找到就返回对应的下标,如果找不到,就返回-1
for(let i=0;i<arr.length;i++){
const Index = arr2.findIndex((item) => {return item.key === arr[i].key})
console.log(Index); // 0、1、-1、-1、-1
// 前两个数据的key值相同,打印出对应的下标 ,后面找不相同的key 输出-1
} //两个数组对象中查看 其中一个数组对象是否包含另一个数组对象中的值
let arr = [
{key:1,val:"张胜男"},
{key:2,val:"王五"},
{key:3,val:"李四"},
{key:4,val:"刘超"},
{key:5,val:"赵四"}
]
let arr2 = [
{key:1,val:"张胜男"},
{key:2,val:"王五"},
{key:6,val:"666"},
{key:7,val:"777"},
{key:8,val:"888"},
{key:9,val:"999"},
]
for(let task of arr){
const itemJson = arr2.find((item)=>{return item.key === task.key});
console.log(itemJson)
//{key: 1, val: "张胜男"}
//{key: 2, val: "王五"}
// 3 undefined
// 如果有相同的值那么就返回对应的对象 如果没有则返回 undefined
}上述两种方法都能找出 一个数组对象中是否包含另一个数组对象中的元素,两种方法返回的结果不同,一个是返回下标,另外返回对应的对象,在实际应用场景中可根据不同需要选择不同的使用方式。
边栏推荐
- Common coding and encryption in penetration testing
- The seminar on "global IPv6 development and outlook 2020-2021" was held in Beijing
- Spark stage and shuffle for daily data processing
- atguigu----15-内置指令
- [learn FPGA programming from scratch -41]: vision chapter - Moore's era and Moore's law and the arrival of the post Moore Era
- PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
- 自动化测试的生命周期是什么?
- L2TP connection failure guide in VPN
- RDD basic knowledge points
- New features of PHP: bytecode cache and built-in server
猜你喜欢

光照使用的简单总结

Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application

【Django中运行scrapy框架,并将数据存入数据库】

面试中的最常被问到的两种锁
![[GUET-CTF2019]zips](/img/79/22ff5d4a3cdc3fa9e0957ccc9bad4b.png)
[GUET-CTF2019]zips

More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?

Win10 build webservice
![[wustctf2020] climb](/img/b6/4a0582144c3125e7a0666bbbbfe29d.png)
[wustctf2020] climb

How can win11 set the CPU performance to be fully turned on? How does win11cpu set high performance mode?
![[mrctf2020] thousand layer routine](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[mrctf2020] thousand layer routine
随机推荐
Combine with (& &) logic or (||), dynamic binding and ternary operation
[wustctf2020] climb
Teach you how to use the reflect package to parse the structure of go - step 2: structure member traversal
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence
New ways to play web security [6] preventing repeated use of graphic verification codes
Maxcompute remote connection, uploading and downloading data files
图形技术之坐标转换
Tidb operator source code reading (IV) control cycle of components
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
Super fast reading in OI
How to delete / select an input method on your computer
PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
Blue Bridge Cup seven segment code (dfs/ shape pressing + parallel search)
POM configuration provided and test
Shader 常用函数
A case of bouncing around the system firewall
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
爬虫基础B1——Scrapy(B站学习笔记)