I have a number of tables to which I push Column Values that I have on file, however I often times end up with multiple of the same value, which doesn't look great. I want to reduce it so that these values can only appear once, I have just done this with another table in my project, but the data is gathered differently for this table and I'm not sure what is the best way to work with my Column Values within the already existing method to filter it to only 1 of each identical value, rather than create a new method, any tips would be appreciated. I'll attach what I did with my other table and my current problem below.
Current Table Data Gathering:
participantDetails(participant) {
return [ // take all column values, move into arrays, take out duplicates,
{
columnName: I18n.t('ccenter.case_file.table.role'),
columnValue: participant.role,
dataPointName: 'role'
},
{
columnName: I18n.t('ccenter.case_file.table.company_names'),
columnValue: participant.companyNames.join(', ') || '-',
dataPointName: 'companyName'
},
{
columnName: I18n.t('ccenter.case_file.table.aliases'),
columnValue: participant.aliases.join(', ') || '-',
dataPointName: 'name'
},
{
columnName: I18n.t('ccenter.case_file.table.birthdates'),
columnValue: participant.birthdates.join(', ') || '-',
dataPointName: 'birthdate'
},
{
columnName: I18n.t('ccenter.case_file.table.phones'),
columnValue: participant.phones.join(', ') || '-',
dataPointName: 'phone'
},
{
columnName: I18n.t('ccenter.case_file.table.emails'),
columnValue: participant.emails.join(', ') || '-',
dataPointName: 'email'
},
{
columnName: I18n.t('ccenter.case_file.table.addresses'),
columnValue: participant.addresses.join(', ') || '-',
dataPointName: 'address'
},
{
columnName: I18n.t('ccenter.case_file.table.customer_numbers'),
columnValue: participant.customerNumbers.join(', ') || '-',
dataPointName: 'customerNumber'
}
]
},
Previous Table:
fields.forEach(field => {
if (participant[field.fieldName].length > 0) {
const uniqueValues = [];
const uniqueElements = [];
participant[field.fieldName].forEach(element => {
if (!uniqueValues.includes(element.value)) {
uniqueValues.push(element.value);
uniqueElements.push(element);
}
})
result.push({
attributeName: field.fieldName,
columnName: I18n.t(`ccenter.participant.table.${snakeCase(field.fieldName)}`),
columnValues: uniqueElements,
fieldClass: field.fieldClass,
})
}
});
return result;
}
Before calling participantDetails(participant) you can remove all duplicates values from participant.
First get all keys from participant then check if its value is array, If yes then call removeDuplicates to remove duplicates from that array.
function removeDuplicateValues(p){
return Object.keys(p).map(k => Array.isArray(p[k]) ? { [k]: removeDuplicates(p[k]) } : { [k]: p[k] }).reduce((acc, item) => ({...acc, ...item}), {});
}
function removeDuplicates(arr){
return [...new Set(arr)];
}
const participant = {
role: 'Role_A',
companyNames: ['Company1', 'Company2', 'Company3', 'Company1'],
aliases: ['aliases1', 'aliases2', 'aliases3', 'aliases1'],
birthdates: ['birthdates1', 'birthdates2', 'birthdates3', 'birthdates2'],
phones: ['phones-1', 'phones-2', 'phones-3', 'phones-2'],
emails: ['emails-1', 'emails-2', 'emails-3', 'emails-3'],
addresses: ['addresses-1', 'addresses-2', 'addresses-3', 'addresses-1'],
customerNumbers: ['customerNumbers-1', 'customerNumbers-2', 'customerNumbers-3', 'customerNumbers-4']
};
const result = removeDuplicateValues(participant);
console.log(result);
Use removeDuplicateValues function inside participantDetails:
participantDetails(participant) {
participant = removeDuplicateValues(participant);
return [
...
];
}