You can achieve this by using the 'filter' method to remove duplicate signatory names before mapping them. Here's an updated version of the code:

if (!Array.isArray(list)) return '';
return list.map((item) => {
  const uniqueSignatories = item.signatories.filter((signatory, index, array) => {
    return array.findIndex((s) => s.signatoryName === signatory.signatoryName) === index;
  });

  return uniqueSignatories.map((signatory) => (
    <span>{signatory?.signatoryName}</span>
  ));
});

This code filters out duplicate signatory names using the 'filter' method along with the 'findIndex' method. The 'findIndex' method checks if the current signatory's name has already been encountered before in the array. If it hasn't, it returns the current index, indicating that it is the first occurrence. Only the signatories with unique names are then mapped and returned as a list of <span> elements.

Remove Duplicate Signatory Names in Javascript Array

原文地址: https://www.cveoy.top/t/topic/eag8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录