Reputation: 1188
I have this function:
private sectionIndex: number; <--- set above my constructor.
The items of the sections object are thus:
public sections = {
pi: [ **<-- Want to remove**
{title: 'Employee Information'},
{title: 'Address'},
{title: 'Summary'},
],
ee: [ **<-- Want to remove**
{title: 'Employee Eligibility Information'},
{title: 'Summary'},
],
pd: [ **<-- Want to remove**
{title: 'Payroll Information'},
{title: 'Summary'},
],
es: [ **<-- Want to remove**
{title: 'Esignature'},
{title: 'Summary'},
]
};
Would like to combine like so:
public sections = {
s: [
{title: 'Employee Information'},
{title: 'Address'},
{title: 'Employee Eligibility Information'},
{title: 'Summary'}, <-- Common between all...
{title: 'Payroll Information'},
{title: 'Esignature'}
]
};
This is the function that I'd like to minimize:
public somefunction() {
let result = true;
if (this.sectionIndex === this.sections.pi.length - 1 ||
this.sectionIndex === this.sections.pd.length - 1 ||
this.sectionIndex === this.sections.ee.length - 1 ||
this.sectionIndex === this.sections.es.length - 1) {
result = false;
console.log('There are no more sections');
} else {
this.sectionIndex++;
if (this.sections.pi.length - 1) {
console.log('First section is ' +
this.sections.pi[this.sectionIndex].title + ' which is ' +
this.sections.pi[this.sectionIndex].value);
} else if (this.sections.pd.length - 1) {
console.log('First section is ' +
this.sections.pd[this.sectionIndex].title + ' which is ' +
this.sections.pd[this.sectionIndex].value);
} else if (this.sections.pi.length - 1) {
console.log('First section is ' +
this.sections.ee[this.sectionIndex].title + ' which is ' +
this.sections.ee[this.sectionIndex].value);
} else if (this.sections.es.length - 1) {
console.log('First section is ' +
this.sections.es[this.sectionIndex].title + ' which is ' +
this.sections.es[this.sectionIndex].value);
}
}
return result;
}
I'd like to minimize the code above to something more, palatable whereas I'm not having to do the "OR's" and if then else, else if's, blah, blah, blah...
Also, the sections (is hard coded for now) and will not always be the same. There may be more, there may be less.
What is similar is the SUMMARY section. SUMMARY will appear in all sections
My question, how can I combine the sections object and remove the "pi, ee, pd, and es?" using only the sub-items to traverse?
Upvotes: 0
Views: 161
Reputation: 37918
For combining the sections you could flatten the object values. Then to get rid of duplicates you can use set to keep already added entries:
const sections = {
pi: [
{title: 'Employee Information'},
{title: 'Address'},
{title: 'Summary'},
],
ee: [
{title: 'Employee Eligibility Information'},
{title: 'Summary'},
],
pd: [
{title: 'Payroll Information'},
{title: 'Summary'},
],
es: [
{title: 'Esignature'},
{title: 'Summary'},
]
};
const combined = Object.values(sections).flat();
const addedTitles = new Set();
const noDuplicates = [];
combined.forEach(item => {
if (!addedTitles.has(item.title)){
noDuplicates.push(item);
addedTitles.add(item.title)
}
});
const result = { s: noDuplicates };
console.log(result);
If you don't expect the list to be big, you can check if item is already in the list using find (without adding a title set). Something like:
const combined = Object.values(sections).flat().reduce((result, item) => {
if (!result.find(i => i.title === item.title)) {
result.push(item);
}
return result;
}, []);
Upvotes: 1