当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
本地备份和还原 SQL Server 数据库
Pyhton crawls to Adu (Li Yifeng) Weibo comments
Part 2: drawing a window
The two most frequently asked locks in the interview
Specify IP when calling feign interface
慕思股份在深交所上市:毛利率持续下滑,2022年一季度营销失利
First acquaintance with JUC - day01
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
调用Feign接口时指定ip
L1-019 who goes first (15 points)
pair类备注
OpenGauss数据库在 CentOS 上的实践,配置篇
希尔伯特-黄变换
Common array encapsulation
Wechat cloud hosting hot issues Q & A
没有专业背景,还有机会成为机器学习工程师吗?
线程注意事项
exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
第 2 篇:繪制一個窗口









