Saya memiliki objek yang ingin saya hapus elemen dari salah satu propertinya yang merupakan array objek berdasarkan properti yang cocok dari objek luar.

Ini menggunakan npm deep-diff untuk membandingkan dua objek.

Masalah saya ada di dalam combineDuplicateRecords, ia membandingkan setiap catatan dengan setiap catatan, membuat duplikat dalam larik identitas. Jadi identitas akan terlihat seperti:

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

Ketika saya benar-benar ingin terlihat seperti ini:

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

Kode:

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)));
3
Josh Adams 19 Maret 2020, 23:09

1 menjawab

Jawaban Terbaik

Ambil setiap id dalam dan simpan dalam struktur data, lalu gunakan Array#find untuk menemukan seluruh objek dan masukkan kembali ke 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)
1
Andrew 19 Maret 2020, 21:48