I am seeking clarification on how to handle mock data that includes multiple reviews for each product.
The review_id
is incremented based on the primary key, while the product_id
may have duplicate values due to multiple reviews being associated with the same product. Here's an example:
const data = [
{ review_id: 1, product_id: 1 },
{ review_id: 2, product_id: 1 },
{ review_id: 3, product_id: 2 },
{ review_id: 4, product_id: 2 },
{ review_id: 5, product_id: 3 },
{ review_id: 6, product_id: 3 },
(...)
];
I attempted to generate objects in an array using a nested loop:
const reviewLength = 10;
const productLength = 2;
const mappedReview = [];
for (let i = 1; i <= reviewLength; i++) {
for (let j = 1; j <= productLength; j++) {
const review_id = i * j;
const product_id = j;
mappedReview[i * j - 1] = {
review_id,
product_id
};
}
}
console.log(mappedReview);
However, instead of generating objects, the console output was:
[ { review_id: 1, product_id: 1 },
{ review_id: 2, product_id: 1 },
{ review_id: 3, product_id: 1 },
{ review_id: 4, product_id: 1 },
{ review_id: 5, product_id: 1 },
{ review_id: 6, product_id: 1 },
{ review_id: 7, product_id: 1 },
{ review_id: 8, product_id: 1 },
{ review_id: 9, product_id: 1 },
{ review_id: 10, product_id: 1 },
<1 empty item>,
{ review_id: 12, product_id: 2 },
<1 empty item>,
{ review_id: 14, product_id: 2 },
<1 empty item>,
{ review_id: 16, product_id: 2 },
<1 empty item>,
{ review_id: 18, product_id: 2 },
<1 empty item>,
{ review_id: 20, product_id: 2 } ]
It appears that the loops were executed correctly, but the presence of <1 empty item>
suggests null values in the output.