가라테-자바 스크립트와 유사한 가라테에서 각 중첩을 사용하는 방법
Nov 27 2020
중첩 된 json 구조 아래를 반복하고 모든 필수 필드를 업데이트하고 싶지만 typescript를 통해 이것을 달성 할 수는 있지만 karate JS에서 이것을하고 싶습니다. 각 작업에 대해 중첩하는 방법에 대한 예는 보이지 않습니다.
기간 필드를 업데이트하려는 인덱스를 기반으로 26 개의 기간 데이터 (여기서는 가독성을 위해 3을 사용했습니다)를 업데이트하고 싶습니다. 여러 자동차 및 여러 자동차 속성 및 각 자동차 속성에 26 개의 기간 데이터가 있음)
이 가라테를 사용할 수 없습니다 - 배열 목록이 하나이고 데이터가 적을 때만 두 개의 동적 응답을 일치시킵니다.
[
{
"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;
}
});
}
});
});
});
답변
1 PeterThomas Nov 27 2020 at 10:47
이것을 업데이트가 아니라 변형이라고 생각하십시오. 불필요하게 복잡하기 때문에 귀하의 예를 사용하지 않습니다. 다음은 필요한 모든 개념을 제공하는 더 간단한 예입니다.
* 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"
}
]
}
]
이것이 작동하지 않으면 다른 도구를 찾으십시오. Karate는 일반적으로 프로그래밍 언어에서 수행하는 작업이 아니라 테스트 및 주장을 위해 설계되었습니다. 그리고 나는 당신이 "과도한 스마트 테스트"를 작성하는 함정에 빠졌다고 생각하므로 이것을 읽으십시오 :https://stackoverflow.com/a/54126724/143475