首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用循环中的数字递增JS key

使用循环中的数字递增JS key
EN

Stack Overflow用户
提问于 2021-03-17 21:18:19
回答 1查看 27关注 0票数 0

我嵌套了第二个for循环,并尝试为一年中的所有52周创建wk1wk2等数组键。然后将其添加到item对象中。

期望的结果:

代码语言:javascript
复制
item.wk1 = phpjs.amazon_ratings[i].wk1;
item.wk2 = phpjs.amazon_ratings[i].wk2;
etc...
代码语言:javascript
复制
var i;
var j;
for (i = 0; i < phpjs.amazon_ratings.length; i++) {

  // Create the item object
  var item = {};

  // Add default array keys `sku`, and their values from the `amazon_ratings` array as we loop through each item.
  item.sku = phpjs.amazon_ratings[i].sku;
  item.asin = phpjs.amazon_ratings[i].asin;
  item.current_week_rating = phpjs.amazon_ratings[i].rating;
  item.total_ratings = phpjs.amazon_ratings[i].ratings_total;
  item.category = phpjs.amazon_ratings[i].category;

  // Loop 52 times and create array keys such as: WK1, WK2, WK3
  for (j = 1; j < 53; j++) {
    item.wk[j] = phpjs.amazon_ratings[i].wk[j];
  }

  // Add it to the array
  tabledata.push( item );
}

然而,它失败的原因是:Uncaught TypeError: Cannot set property '1' of undefined

EN

回答 1

Stack Overflow用户

发布于 2021-03-17 21:21:23

它应该是项目‘’wk‘+ j。

记住,一年没有52个星期。你必须每年检查一次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66673920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档