【问题标题】:Karate - How to use nested for each in karate similar to javascript [duplicate]空手道 - 如何在空手道中为每个嵌套使用类似于 javascript [重复]
【发布时间】:2020-11-27 09:40:04
【问题描述】:

我想遍历下面的嵌套 json 结构并想要更新所有必需的字段,但是我可以通过打字稿来实现这一点,但想在空手道 JS 中做到这一点,我没有看到任何示例如何为每个作品嵌套。

我想更新 26 个周期数据(为了便于阅读,我使用了 3 个),基于我要更新周期字段的索引,即 if(index == key),这 26 个周期在每个汽车属性下。(注意:您再次拥有多辆汽车和多个汽车属性,每个汽车属性都有 26 个周期数据)

只有当您有单个数组列表并且数据较少时,我才能使用此 Karate - Match two dynamic responses

  [
      {
        "cars": [
          {
            "name": "car 1",
            "periodsData": [
              {
                "period": "5ed73ed31a775d1ab0c9fb5c",
                "index": 1
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5d",
                "index": 2
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5e",
                "index": 3
              }
            ]
          },
          {
            "name": "car 2",
            "periodsData": [
              {
                "period": "5ed73ed31a775d1ab0c9fb5c",
                "index": 1
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5d",
                "index": 2
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5e",
                "index": 3
              }
            ]
          },
          {
            "name": "car 3",
            "periodsData": [
              {
                "period": "5ed73ed31a775d1ab0c9fb5c",
                "index": 1
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5d",
                "index": 2
              },
              {
                "period": "5ed73ed31a775d1ab0c9fb5e",
                "index": 3
              }
            ]
          }
        ],
        "totalPeriodEprps": [
          {
            "period": "5ed73ed31a775d1ab0c9fb5c",
            "index": 1
          },
          {
            "period": "5ed73ed31a775d1ab0c9fb5d",
            "index": 2
          },
          {
            "period": "5ed73ed31a775d1ab0c9fb5e",
            "index": 3
          }
        ]
       }
     carId ="dfd"
    ]

This above array repeats

输入脚本代码


//periods is a map of index and values

  async modifyCarsData(mid, id, periods, campaignData) {

//carData is a json file
    carData.forEach(element => {
      element.carId= id;

      // Update all egrp periods
      element.totalPeriodEGRPs.forEach(eGrpPeriod => {
        // egrprd.period =
        if (periods.size === element.totalPeriodEGRPs.length) {
          periods.forEach((value, key) => {
            if (key === eGrpPeriod.index.toString()) {
              eGrpPeriod.period = value;
              return true;
            }
          });
        }
      });

      element.cars.forEach(carCell => {
        

        // Logic for updating periods data
        carCell .periodsData.forEach(periodAttribute => {
          if (periods.size === carCell.periodsData.length) {
            periods.forEach((value, key) => {
              if (key === periodAttribute.index.toString()) {
                periodAttribute.period = value;
                return true;
              }
            });
          }
        });
      });
    });

【问题讨论】:

    标签: karate


    【解决方案1】:

    不要认为这是一次更新,而是一次转变。我没有使用您的示例,因为它不必要地复杂。下面是一个更简单的示例,可为您提供所需的所有概念:

    * def data = [{ name: 'one', periods: [{ index: 1, value: 'a' },{ index: 2, value: 'b' }]}, { name: 'two', periods: [{ index: 1, value: 'c' },{ index: 2, value: 'd' }]}]
    * def fnPeriod = function(x){ x.value = x.value + x.index; return x }
    * def fnData = function(x){ return { name: x.name, periods: karate.map(x.periods, fnPeriod) } }
    * def converted = karate.map(data, fnData)
    * print converted
    

    哪些打印:

    [
      {
        "name": "one",
        "periods": [
          {
            "index": 1,
            "value": "a1"
          },
          {
            "index": 2,
            "value": "b2"
          }
        ]
      },
      {
        "name": "two",
        "periods": [
          {
            "index": 1,
            "value": "c1"
          },
          {
            "index": 2,
            "value": "d2"
          }
        ]
      }
    ]
    

    如果这对您不起作用,请寻找其他工具。空手道是为测试和断言而设计的,而不是做你通常在编程语言中做的事情。而且我怀疑你已经落入了编写“过度智能测试”的陷阱,所以请阅读:https://stackoverflow.com/a/54126724/143475

    另请参阅:https://stackoverflow.com/a/53120851/143475

    【讨论】:

    • 感谢您的输入,但我在这里没有做任何花哨的事情,我需要发送的请求 json 以进行 post call 本身很复杂,我会看看这个,看看它是否作品
    • @vinodkomeershetty 听起来你还没有阅读链接,(提示 - 硬编码可以用于测试)无论如何也请阅读:stackoverflow.com/help/someone-answers
    猜你喜欢
    • 1970-01-01
    • 2022-12-09
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多