I'm trying to iterate through objects in an object. I want to only push the properties of the objects into a new array, but only properties with unique hash. I would like to find a solution that will do that inside the for...in.
This solution only pushes the unique hashes, but not the unique "prop.property" objects
for (const prop in this.totals) {
this.totals[prop].map(prop => this.totalProperties.indexOf(prop.property.hash) === -1 ?
this.totalProperties.push(prop.property.hash) : null
);
}
I tried this other one, but it pushes all objects, not unique ones.
for (const prop in this.totals) {
this.totals[prop].map(prop => this.totalProperties.indexOf(prop.property.hash) === -1 ?
this.totalProperties.push(prop.property) : null
);
}
This is a solution, where I get all the properties objects into a new array. After that, I filter the array setting only the unique objects by hash. Is there a better way to filter them when pushing into this.totalProperties array? Therefore, I would not have to push them all, and then filter by hash. I want to filter while pushing.
for (const prop in this.totals) {
this.totals[prop].map(prop =>
this.totalProperties.push(prop.property));
}
const generalProps = [...new Map(this.totalProperties.map(item => [item.hash, item])).values()];