|
.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.
Reviews-api/server/controllers/index.js
Lines 107 to 138 in 233b9ca
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
countReviewssince its not obvious from the code what is happening in this.then()block.