簡體   English   中英

根據外部對象屬性刪除對象上數組的元素

[英]Remove Elements of an array on an object based on outer object property

我有一個對象,我想從其屬性之一中刪除元素,該屬性是基於外部對象的匹配屬性的對象數組。

這使用 npm deep-diff 來比較兩個對象。

我的問題是在 combineDuplicateRecords 內部,它將每條記錄與每條記錄進行比較,在 identities 數組中創建重復項。 所以身份最終看起來像:

[{
    id: "111",
    identities: [
      {
        id: "111"
      },
      {
        id: "111"
      },
      {
        id: "222"
      },
      {
        id: "222"
      },
      {
        id: "333"
      },
      {
        id: "333"
      }
    ]
}]

當我真的希望它看起來像這樣時:

[{
id: "111",
identities:[
      {
        id: "222"
      },
      {
        id: "333"
      }
    ]
}]

代碼:

var requestRecords = [
      {
        vid: "12345",
        id: "12345",
        email: "gft@test.com",
        firstName: "GrandFathering",
        lastName: "TestMN",
        postalCode: "55443-2410",
        phone: "123-456-7890",
        key: "1212"
      },
      {
        vid: "121212",
        id: "12222",
        email: "g233@test.com",
        firstName: "NoMatch",
        lastName: "NoMatchFound",
        postalCode: "43233-2410",
        phone: "123-456-7890",
        key: "121233"
      },
      {
        vid: "111",
        id: "111",
        email: "ffffft@test.com",
        firstName: "samebatch",
        lastName: "samebatch",
        postalCode: "5545",
        phone: "123-456-7890",
        key: "3333",
      },
      {
        vid: "222",
        id: "222",
        email: "ffffft@test.com",
        firstName: "samebatch",
        lastName: "samebatch",
        postalCode: "5545",
        phone: "123-456-7890",
        key: "4444",
      },
      {
        vid: "333",
        id: "333",
        email: "ffffft@test.com",
        firstName: "samebatch",
        lastName: "samebatch",
        postalCode: "5545",
        phone: "123-456-7890",
        key: "55",

      }
    ];


  combineDuplicateRecords = (arrayOfRecords, prefilter) => {
    const recordsToRemove = [];
    arrayOfRecords.forEach(firstRecord => {
      arrayOfRecords.forEach((secondRecord, index) => {
        if (
          firstRecord.firstName == secondRecord.firstName &&
          firstRecord.lastName == secondRecord.lastName &&
          firstRecord.dateOfBirth == secondRecord.dateOfBirth &&
          firstRecord.phone == secondRecord.phone &&
          firstRecord.postalCode == secondRecord.postalCode &&
          firstRecord.id !=
            secondRecord.id
        ) {
          const identities = [];
          let identity = {};
          this.preserveExisitingIdentities(secondRecord, identities);
          this.preserveExisitingIdentities(firstRecord, identities);
          identity = this.setIdentityDifferencesBetweenRecords(
            firstRecord,
            secondRecord,
            prefilter,
            identity
          );
          identities.push(identity);
          firstRecord["identities"] = identities;
          recordsToRemove.push(index);
        }
      });
    });

    [...new Set(recordsToRemove)].forEach(index => {
      arrayOfRecords.splice(index, 1);
    });
    return arrayOfRecords;
  };

    preserveExisitingIdentities = (record, identities) => {
    if (record.hasOwnProperty("identities")) {
      record.identities.forEach(identity => {
        identities.push(identity);
      });
    }
    return identities;
  };

    setIdentityDifferencesBetweenRecords = (
    firstIdentity,
    secondIdentity,
    prefilter,
    identity
  ) => {
    const differences = Diff(firstIdentity, secondIdentity, prefilter);
    let i = differences.length;
    while (i--) {
      if (differences[i].path[0] == "vid") {
        differences.splice(i, 1);
      }

      if (differences[i].path[0] == "identities") {
        differences.splice(i, 1);
      }
      //we only want to keep the differences so we remove kind D
      if (differences[i]?.kind == "D") {
        differences.splice(i, 1);
      }
    }
    differences.forEach(diff => {
      identity[diff.path[0]] = diff.lhs;
    });
    return identity;
  };
  console.log(JSON.stringify(combineDuplicateRecords(requestRecords)));

獲取每個內部 id 並將它們保存在一個數據結構中,然后使用Array#find查找整個對象並將其插入到identities

 const array = [ { id: "111", identities: [ { id: "111" }, { id: "111" }, { id: "222" }, { id: "222" }, { id: "333" }, { id: "333" } ] } ] const cleanObject = (obj) => { const allIds = obj.identities.map(({ id }) => id) const mainId = obj.id const uniqueIds = new Set(allIds) uniqueIds.delete(mainId) const nextIdentities = [...uniqueIds].map(currId => { return obj.identities.find(({ id }) => currId === id) }) obj.identities = nextIdentities return obj }; const el = array.map(entry => { return cleanObject(entry) }) console.log(el)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM