当前位置:网站首页>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
}上述两种方法都能找出 一个数组对象中是否包含另一个数组对象中的元素,两种方法返回的结果不同,一个是返回下标,另外返回对应的对象,在实际应用场景中可根据不同需要选择不同的使用方式。
边栏推荐
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
- 阿里云全链路数据治理
- 语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
- Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
- 10 common malware detection and analysis platforms
- Wechat cloud hosting hot issues Q & A
- 2022年PMP项目管理考试敏捷知识点(1)
- Global and Chinese market of digital fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- 《canvas》之第3章 曲线图形
- Analog display of the module taking software verifies the correctness of the module taking data, and reversely converts the bin file of the lattice array to display
猜你喜欢

爬虫基础B1——Scrapy(B站学习笔记)

相機標定(標定目的、原理)

Oracle-高级SQL限定查询

Étalonnage de la caméra (objectif et principe d'étalonnage)
![[mrctf2020] thousand layer routine](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[mrctf2020] thousand layer routine
![[pointnet] matlab simulation of 3D point cloud target classification and recognition based on pointnet](/img/86/5db689cdac2a927a23dff3fb9594b0.png)
[pointnet] matlab simulation of 3D point cloud target classification and recognition based on pointnet
![[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design](/img/74/f615191715a9ac58a8546f8d1e8f8d.png)
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
![Selector (>, ~, +, [])](/img/7e/2becfcf7a7b2e743772deee5916caf.png)
Selector (>, ~, +, [])

面试中的最常被问到的两种锁
![[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog](/img/38/92486c92557e6e5a10a362eb2b7bdf.png)
[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
随机推荐
2022年PMP项目管理考试敏捷知识点(1)
MySQL case: analysis of full-text indexing
图形技术之管线概念
Anaconda 中使用 You Get
A summary of the posture of bouncing and forwarding around the firewall
How to delete / select an input method on your computer
Description of module data serial number positioning area code positioning refers to GBK code
Shell script for MySQL real-time synchronization of binlog
Take my brother to make a real-time Leaderboard
报错“Computation failed in `stat_summary_hex()`”
Phonics
A penetration test of c/s Architecture - Request encryption, decryption and test
UE常用控制台命令
Super fast reading in OI
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
Global and Chinese markets for maritime transport of perishable goods 2022-2028: Research Report on technology, participants, trends, market size and share
Session & cookie details
Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
MFC multithreaded semaphore csemaphore critical area and mutually exclusive events
UE常用控制臺命令