Karate 空手道-如何使用嵌套的空手道类似于javascript
我想在下面的嵌套json结构中循环,并想更新所有必需的字段,但是我可以通过typescript实现这一点,但我想在KarateJS中实现这一点,我没有看到任何示例如何为每个工作嵌套 我想更新26个周期数据(为了可读性,这里我使用了3),基于我想更新周期字段的索引,即如果(index==key),这26个周期位于每个汽车属性下。(注意:您同样有多个汽车和多个汽车属性,每个汽车属性有26个周期数据) 我不能仅当您有单个数组列表且数据较少时才使用此选项Karate 空手道-如何使用嵌套的空手道类似于javascript,karate,Karate,我想在下面的嵌套json结构中循环,并想更新所有必需的字段,但是我可以通过typescript实现这一点,但我想在KarateJS中实现这一点,我没有看到任何示例如何为每个工作嵌套 我想更新26个周期数据(为了可读性,这里我使用了3),基于我想更新周期字段的索引,即如果(index==key),这26个周期位于每个汽车属性下。(注意:您同样有多个汽车和多个汽车属性,每个汽车属性有26个周期数据) 我不能仅当您有单个数组列表且数据较少时才使用此选项 [ { &quo
[
{
"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;
}
});
}
});
});
});
不要认为这是一种更新,而是一种转换。我不使用你的例子,因为它不一定复杂。下面是一个简单的示例,它为您提供了所需的所有概念:
* 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"
}
]
}
]
如果这对您不起作用,请寻找其他工具。空手道是为测试和断言而设计的,而不是像编程语言中通常做的那样。我怀疑您已经落入了编写“过度智能测试”的陷阱,因此请阅读以下内容:感谢您的输入,但我在这里没有做任何花哨的事情,我需要为post调用发送的json请求本身就很复杂,不管怎样,我会看看这个,看看它是否works@vinodkomeershetty听起来你好像还没有读过这些链接,(提示-硬编码可以用于测试)无论如何,请也阅读以下内容: