Javascript:创建具有多个循环的对象

时间:2019-06-20 06:48:23

标签: javascript object for-loop

我有一个关于模拟数据的问题,每个产品都有多个评论。

review_id以pk的顺序递增,并且product_id将具有重复的值,因为同一产品上可能有多个评论。如下图所示:

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 },

 (...)

];

我尝试使用双循环在数组中创建对象:

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);

但是它不是对象,而是打印在控制台上的,如下所示:

[ { 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 } ]

您似乎已正确执行了循环<1 empty item>(由于文件中写入的内容,它显示为null)。

1 个答案:

答案 0 :(得分:0)

由于您的review_id总是增加,所以我只使用一个循环并在每次输出product_id项时递增productLength

const reviewLength = 10;
const productLength = 2;

const mappedReview = [];
let product_id = 1;
let product_counter = 1; // Used to tell when to increment product_id
for (let review_id = 1; review_id <= reviewLength; review_id++) {
    // Add this object
    mappedReview.push({review_id, product_id});
    // Increment product_id if appropriate
    if (product_counter++ === productLength) {
        ++product_id;
        product_counter = 1;
    }
}

console.log(mappedReview);
.as-console-wrapper {
    max-height: 100% !important;
}

或者使用product_id而不是计数器来计算:

const reviewLength = 10;
const productLength = 2;

const mappedReview = [];
for (let review_id = 1; review_id <= reviewLength; review_id++) {
    // Add this object
    mappedReview.push({
        review_id,
        product_id: Math.ceil(review_id / productLength)
     });
}

console.log(mappedReview);
.as-console-wrapper {
    max-height: 100% !important;
}


在您询问的评论中:

  

我可以使用数组方法map,reduce等表达这种情况吗?

您可以将它塞入map调用中(通过创建一个数组,fill对其进行命名,然后使用map),但是可以使用{{3}的映射回调功能}会更有意义:

const reviewLength = 10;
const productLength = 2;

const mappedReview = Array.from({length: reviewLength}, (_, index) => ({
    review_id: index + 1,
    product_id: Math.ceil((index + 1) / productLength)
}));

console.log(mappedReview);
.as-console-wrapper {
    max-height: 100% !important;
}

要在其中刺map,请Array.from({length: reviewLength},与(Array(reviewLength).fill().map(

const mappedReview = Array(reviewLength).fill(0).map((_, index) => ({
    // ...
}));