如何检查数组中的任何元素是否包含在另一个数组中

时间:2019-10-30 14:12:45

标签: javascript arrays

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

 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为true,如果其中一个不包含,则为false。

我认为我需要一些运算符

1 个答案:

答案 0 :(得分:0)

我使用所有数组函数的方法:

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)
}))