当前位置:网站首页>JS implementation to check whether an array object contains values from another array object
JS implementation to check whether an array object contains values from another array object
2022-06-24 07:52:00 【Zhangxiaolang】
// Two array objects Whether one of the array objects contains values from the other array object
let arr = [
{key:1,val:" Zhang Shengnan "},
{key:2,val:" Wang Wu "},
{key:3,val:" Li Si "},
{key:4,val:" Liu chao "},
{key:5,val:" Zhao si "}
]
let arr2 = [
{key:1,val:" Zhang Shengnan "},
{key:2,val:" Wang Wu "},
{key:6,val:"666"},
{key:7,val:"777"},
{key:8,val:"888"},
{key:9,val:"999"},
]
// If it can be found, the corresponding subscript will be returned , If you can't find it , Just go back to -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
// Of the first two data key Same value , Print out the corresponding subscript , Find something different in the back key Output -1
} // Two array objects Whether one of the array objects contains values from the other array object
let arr = [
{key:1,val:" Zhang Shengnan "},
{key:2,val:" Wang Wu "},
{key:3,val:" Li Si "},
{key:4,val:" Liu chao "},
{key:5,val:" Zhao si "}
]
let arr2 = [
{key:1,val:" Zhang Shengnan "},
{key:2,val:" Wang Wu "},
{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: " Zhang Shengnan "}
//{key: 2, val: " Wang Wu "}
// 3 undefined
// If there is the same value, the corresponding object is returned If not, return undefined
}Both of the above methods can find Whether an array object contains elements from another array object , The two methods return different results , One is the return subscript , In addition, the corresponding object is returned , In the actual application scenario, different usage modes can be selected according to different needs .
边栏推荐
- Experience of Shenzhou computer
- 图形技术之管线概念
- 基于Distiller的模型压缩工具简介
- Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022
- [C language] system date & time
- uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效
- 没有专业背景,还有机会成为机器学习工程师吗?
- Timer usage notes
- Explain the input attribute in PHP (hide the read-only restriction)
- 毕业两年月薪36k,说难也不难吧
猜你喜欢

5-if语句(选择结构)

Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
![LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路](/img/16/011ba3aef1315c39526daac7e3ec89.png)
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路

Part 2: drawing a window

Baidu map, coordinate inversion, picking coordinate position

OpenGauss数据库在 CentOS 上的实践,配置篇

Chapter 3: drawing triangles

希尔伯特-黄变换

【Django中运行scrapy框架,并将数据存入数据库】
随机推荐
Jenkins is too old try it? Cloud native ci/cd Tekton
uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
AWTK 最新动态:Grid 控件新用法
Unity Culling 相关技术
Random number remarks
Anaconda 中使用 You Get
Experience of Shenzhou computer
随机数备注
The seminar on "global IPv6 development and outlook 2020-2021" was held in Beijing
保留一位小数和保留两位小数
UE常用控制臺命令
Wechat cloud hosting hot issues Q & A
Chapter 2 line graph of canvas
Resolution error: LNK2019 unresolved external symbol
向量操作与坐标转换相关方法
Shader 常用函数
闲谈:3AC到底发生了什么?
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
atguigu----16-自定义指令