当前位置:网站首页>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
}
上述两种方法都能找出 一个数组对象中是否包含另一个数组对象中的元素,两种方法返回的结果不同,一个是返回下标,另外返回对应的对象,在实际应用场景中可根据不同需要选择不同的使用方式。
边栏推荐
- How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
- Counter attack from outsourcing to big factories! Android has been developed for 5 years, and after a year of dormancy, it has tried to become an offer harvester. Tencent has a fixed salary of 20*15
- C escape character
- UE常用控制臺命令
- How to open the soft keyboard in the computer, and how to open the soft keyboard in win10
- 智能指针备注
- Deploy L2TP in VPN (medium)
- Several misunderstandings of VPN
- 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
- Description of module data serial number positioning area code positioning refers to GBK code
猜你喜欢
How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset
Blue Bridge Cup seven segment code (dfs/ shape pressing + parallel search)
[wustctf2020] climb
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
【NILM】非入侵式负荷分解模块nilmtk安装教程
Buuctf misc grab from the doll
Win10 build webservice
云开发谁是卧底小程序源码
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence
Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
随机推荐
Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
Shader 常用函数
自动化测试的生命周期是什么?
[learn FPGA programming from scratch -42]: Vision - technological evolution of chip design in the "post Moorish era" - 1 - current situation
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
What challenges does the video streaming media platform face in transmitting HD video?
Dichotomous special training
UTC、GMT、CST
Event related | reveal how Ti-One's support ability for large-scale events is developed
Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
How to realize high stability and high concurrency of live video streaming transmission and viewing?
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
简单的折射效果
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
L2TP connection failure guide in VPN
位运算
【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
How to realize multi protocol video capture and output in video surveillance system?
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)