【问题标题】:How could I jump number for variable? [duplicate]我怎样才能为变量跳转数字? [复制]
【发布时间】:2020-09-24 15:37:07
【问题描述】:

我的 json 名称如下:

product_1
product_2
product_3

我可以有变量+数字的循环吗?

我的问题是:json.subject__ + (i + 1)

const json = items[0].json["data"];

var count = json.count;

var subject = [];
for (i = 0; i < count; i++) {
  subject[i] = json.subject__ + (i + 1);
}

我的json结构是这样的:

[
  {
    "data": {
      "use_for": "website",
      "product__1": "bai 1",
      "length__1: "option 1",
      "product__2": "bai 2",
      "length__2": "option 2",
      "product__3": "bai 3",
      "length__3": "option 3",
      "format__3": [
         "sss",
         "kkk", ...

【问题讨论】:

  • 请添加您的数据结构。
  • 我猜你想这样做:json['subject__' + (i + 1)] 而不是这个json.subject__ + (i + 1)
  • 顺便说一句,你应该 push()'ing 到一个数组,但你可以简单地使用 map() 而不是 for 循环
  • 我添加了数据结构

标签: javascript json loops


【解决方案1】:

您可以使用括号表示法:

subject[i] = json["subject__" + (i + 1)];

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2019-04-28
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2021-01-19
    • 2015-04-19
    相关资源
    最近更新 更多