本文介绍了空手道-如何在空手道中为每个嵌套使用类似于javascript的空手道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to loop through below nested json structure and want to update all the required fields, however I could achieve this through typescript but want to do this in karate JS, I do not see any examples how to nested for each works.

我想更新26个周期数据(此处为可读性,我使用了3个),基于索引,我想更新周期字段,即if(index == key),这26个周期位于每个汽车属性下.(注意:您又有多辆汽车和多辆汽车属性,而每辆汽车属性都有26个期间数据)

I want to update 26 periods data(here for readability i used 3), based on index I want to update period field, i.e. if(index == key), these 26 periods are under each car attribute.(NOTe: you again have multiple cars and multiple car attributes and each car attribute you have 26 periods data)

我不能使用此空手道-仅当您只有一个时才匹配两个动态响应数组列表且数据较少

I cannot use this Karate - Match two dynamic responses only when you have single array list and have less data

  [
      {
        "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;
              }
            });
          }
        });
      });
    });

推荐答案

不要将其视为更新,而是转换.我不使用您的示例,因为它不必要地复杂.这是一个更简单的示例,为您提供所需的所有概念:

Don't think of this as an update, but as a transform. I'm not using your example because it is un-necessarily complicated. Here is a simpler example that gives you all the concepts you need:

* 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

If this doesn't work for you, please look for another tool. Karate is designed for testing and assertions, not doing what you normally do in programming languages. And I suspect that you have fallen into the trap of writing "over-smart tests", so please read this: https://stackoverflow.com/a/54126724/143475

这篇关于空手道-如何在空手道中为每个嵌套使用类似于javascript的空手道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 08:39