Skip to content

Factor out logic for better readability #4

Description

@cchauche

.then((data) => {
const response = data.map((metaData) => {
const { reviews, characteristics } = metaData;
const ratingObj = reviews.reduce((acc, val) => {
if (acc[val.rating] === undefined) {
acc[val.rating] = 1;
} else {
acc[val.rating] += 1;
}
return acc;
}, {});
const recommendObj = reviews.reduce((acc, val) => {
if (val.recommend === true) {
acc[0] = 1;
} else {
acc[0] += 1;
}
return acc;
}, {});
const characteristicObj = {};
characteristics.forEach((characteristic) => {
characteristicObj[characteristic.name] = {
id: characteristic.id,
value: (characteristic.characteristic_reviews.reduce((acc, val) => {
acc += val.value;
return acc;
}, 0) / characteristic.characteristic_reviews.length).toFixed(4),
};
});

This section of code might be a good candidate to factor our some of the logic to helper functions. By creating functions with descriptive names it will be easier for those reading your code to understand what is goin on. For example you could create a function called countReviews since its not obvious from the code what is happening in this .then() block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions