简体   繁体   English

如何检查一个数组是否包含另一个数组的所有元素

[英]How to check if an array contains all the elements of another another array

I am having two arrays I need to check if Array(A) contains all the elements of Array(B) but Array(A) can have more elements than Array(B) I have tried with the below code but it is not working as expected.我有两个 arrays 我需要检查 Array(A) 是否包含 Array(B) 的所有元素,但是 Array(A) 可以有比 Array(B) 更多的元素我尝试使用下面的代码但它不能作为预期的。

Below is my code下面是我的代码

let ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}];
let ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}];

checkArrayElements() {
    let value: boolean;
    this.ArrayA.map((element) => {
        value =
            this.ArrayB.find(
                (field: any) => field.headerName == element.headerName
            ) != null;
    });
    return value;
}

could anyone please tell me where I am making the mistake?谁能告诉我我在哪里犯了错误?

The main issue with your current approach is that you are re setting value for every iteration of .map() (side note: this should be .forEach() and not .map() since you are not using the result returned by .map() - more info here ).您当前方法的主要问题是您正在为.map()的每次迭代重新设置value (旁注:这应该是.forEach()而不是.map()因为您没有使用.map()返回的结果.map() - 更多信息在这里)。 You are currently only checking if the value of the last element in ArrayA appears in ArrayB as you are not taking into account what the previous values on value were.您目前仅检查ArrayB ArrayA ,因为您没有考虑 value 的先前value是什么。 Instead, you can make your outer loop over array B and then use an inner loop to check if the value from array B appears in A.相反,您可以在数组 B 上创建外部循环,然后使用内部循环检查数组 B 中的值是否出现在 A 中。

With that being said, I think I would use a Set , where you can merge both array elements and remove the duplicates using a Set.话虽如此,我想我会使用Set ,您可以在其中合并两个数组元素并使用 Set 删除重复项。 The size of the Set should equal the size of ArrayA if there all elements from B appear in ArrayA:如果 B 中的所有元素都出现在 ArrayA 中,则 Set 的大小应该等于 ArrayA 的大小:

 const ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}]; const ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}]; const allInBAppearInA = new Set([...ArrayA, ...ArrayB].map(({headerName}) => headerName)).size === ArrayA.length; console.log(allInBAppearInA);

If I was to use a loop approach like explained above, I would use .every() to check that every elements from ArrayB can be found in ArrayA (you can use .some() to check that ArrayA contains an object with a given header name matching one from ArrayA ):如果我要使用如上所述的循环方法,我将使用.every()来检查 ArrayB 中的每个元素是否可以在ArrayA中找到(您可以使用.some()来检查 ArrayA 是否包含具有给定 header 的 object名称与ArrayA的一个匹配):

 const ArrayA = [{headerName:'No'},{headerName:'zahid'},{headerName:'hussain'}]; const ArrayB = [{headerName:'zahid'},{headerName:'abass'},{headerName:'hussain'}]; const res = ArrayB.every(({headerName}) => ArrayA.some(obj => obj.headerName === headerName)); console.log(res);

Using filter (a simplified option)使用过滤器(简化选项)

const value =
      this.ArrayB.filter(f => this.ArrayA.map(m => m.headerName).includes(f.headerName)).length === this.ArrayB.length;
    console.log(value);

You can get diffrences between two arrays like this:您可以像这样获得两个 arrays 之间的差异:

let arrayA = [
    { headerName: "No" },
    { headerName: "zahid" },
    { headerName: "hussain" },
  ];
  let arrayB = [
    { headerName: "zahid" },
    { headerName: "abass" },
    { headerName: "hussain" },
  ];

  let firstArray = [];
  let secondArray = [];
  let diffrence = [];
  arrayA.map((objects) => {
    Object.keys(objects).map((key) => firstArray.push(objects[key]));
  });
  arrayB.map((objects) => {
    Object.keys(objects).map((key) => secondArray.push(objects[key]));
  });

  firstArray.map((element) => {
    if (!secondArray.includes(element)) {
      diffrence.push(element);
    }
  });
  secondArray.map((element) => {
    if (!firstArray.includes(element)) {
      diffrence.push(element);
    }
  });

  console.log(diffrence);

Can work around this simple implementation:可以解决这个简单的实现:

 let ArrayA = [{headerName: 'No'}, {headerName: 'zahid'}, {headerName: 'hussain'}]; let ArrayB = [{headerName: 'zahid'}, {headerName: 'abass'}, {headerName: 'hussain'}]; let checkArrayElements = () => { let allPresent = true; for (let i = 0; i < ArrayB.length; i++) { allPresent = ArrayA.map(a => a.headerName).indexOf(ArrayB[i].headerName);== -1; if (;allPresent) break; } return allPresent. }; console.log(checkArrayElements());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM