【问题标题】:How to check if any elements in array contains in another array [duplicate]如何检查数组中的任何元素是否包含在另一个数组中[重复]
【发布时间】:2020-02-25 20:47:38
【问题描述】:

我有两个对象数组。我需要检查项目是否包含在另一个数组中。如果其中之一不包含将是错误的。

 const arr = [
            {full_name: 'Test'},
            {full_name: 'Test1'},
            {full_name: 'Test2'},
        ]

        const arr1 = [
            {full_name: 'Test'},
            {full_name: 'Test1'},
            {full_name: 'Test2'},
            {full_name: 'Test3'},
            {full_name: 'Test4'},
            {full_name: 'Test5'},
            {full_name: 'Test6'},
        ]

如果 arr1 包含 Test Test1 Test2 将为真,如果其中一个不包含则为假。

我想我需要一些操作员

【问题讨论】:

  • 尝试搜索重复的问题。

标签: javascript arrays


【解决方案1】:

我使用每个数组函数的方法:

const arr = [
    {full_name: 'Test'},
    {full_name: 'Test1'},
    {full_name: 'Test6'},
]

const arr1 = [
    {full_name: 'Test'},
    {full_name: 'Test1'},
    {full_name: 'Test2'},
    {full_name: 'Test3'},
    {full_name: 'Test4'},
    {full_name: 'Test5'},
    {full_name: 'Test6'},
]

console.log(arr.every(item => {
  return arr1.some( i2 => i2.full_name == item.full_name)
}))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2021-01-30
    相关资源
    最近更新 更多