簡體   English   中英

如何檢查一個數組是否包含另一個數組的所有元素

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

我有兩個 arrays 我需要檢查 Array(A) 是否包含 Array(B) 的所有元素,但是 Array(A) 可以有比 Array(B) 更多的元素我嘗試使用下面的代碼但它不能作為預期的。

下面是我的代碼

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

誰能告訴我我在哪里犯了錯誤?

您當前方法的主要問題是您正在為.map()的每次迭代重新設置value (旁注:這應該是.forEach()而不是.map()因為您沒有使用.map()返回的結果.map() - 更多信息在這里)。 您目前僅檢查ArrayB ArrayA ,因為您沒有考慮 value 的先前value是什么。 相反,您可以在數組 B 上創建外部循環,然后使用內部循環檢查數組 B 中的值是否出現在 A 中。

話雖如此,我想我會使用Set ,您可以在其中合並兩個數組元素並使用 Set 刪除重復項。 如果 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);

如果我要使用如上所述的循環方法,我將使用.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);

使用過濾器(簡化選項)

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

您可以像這樣獲得兩個 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);

可以解決這個簡單的實現:

 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