我有一个包含 5 个元素的数组,我想比较其中的索引位置 1 和 2,并根据它是否为整数保留 1 或 2。保留其他元素不变。以下场景:
最终数组应该只有 4 个元素
输入
{
"destination": [
{
"destinationName": ["dest1","dest2","dest3"],
"taxi_cost": [10,20,30],
"van_one_way_cost": [7,6,""],
"van_two_way_cost": [14,"",16],
"subway_cost": [11,22,33],
"bus_cost": [21,22,23]
}
]
如何根据条件比较van_one_way_cost和van_two_way_cost并填充cost数组? 颠簸变形
[
{
"operation": "shift",
"spec": {
"*": "&",
"destination": {
"*": {
"destinationName": {
"*": {
"@": [
"destination.[#2].destName",
"transportList.transportDestination.[#2].name"
]
}
},
"taxi_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"van_one_way_cost": {
"*": {
"@": [
"destination.[#2].owVanFee",
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"van_two_way_cost": {
"*": {
"@": [
"destination.[#2].twVanFee",
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"subway_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
},
"bus_cost": {
"*": {
"@": [
"transportList.transportDestination.[&1].transports.[&1].cost"
]
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"vanFee": ["=toInteger(@(1,owVanFee))", "=(@(1,twVanFee))"]
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"owVanFee": "",
"twVanFee": ""
}
}
}
}
]
我得到的输出
{
"destination" : [
{
"destName": "dest1",
"vanFee" : 7
},
{
"destName": "dest2",
"vanFee" : 6
},
{
"destName": "dest3",
"vanFee" : 16
}
],
"transportList" : {
"transportDestination" : [
{
"name": "dest1",
"transports" : [ {
"cost" : [ 10, 7, 14, 11, 21 ]
}]
},
{
"name": "dest2",
"transports" : [ {
"cost" : [ 20, 6, "", 22, 22 ]
}]
},
{
"name": "dest3",
"transports" : [ {
"cost" : [ 30, "", 16, 33, 23 ]
}]
}
]
}
}
加速输出
{
"destination" : [
{
"destName": "dest1",
"vanFee" : 7
},
{
"destName": "dest2",
"vanFee" : 6
},
{
"destName": "dest3",
"vanFee" : 16
}
],
"transportList" : {
"transportDestination" : [
{
"name": "dest1",
"transports" : [ {
"cost" : [ 10, 7, 11, 21 ]
}]
},
{
"name": "dest2",
"transports" : [ {
"cost" : [ 20, 6, 22, 22 ]
}]
},
{
"name": "dest3",
"transports" : [ {
"cost" : [ 30, 16, 33, 23 ]
}]
}
]
}
}
您可以尝试以下规格:
[
// Merge the van one and two way cost and ignore any empty value
// this will generate an array for each index of none empty values
// where in the next step we will consider only the first element of that array
{
"operation": "shift",
"spec": {
"destination": {
"*": {
"*": "&2[&1].&",
"van_one_way_cost": {
"*": {
"": null,
"*": {
"@1": "&5[&4].van_cost.&2[]"
}
}
},
"van_two_way_cost": {
"*": {
"": null,
"*": {
"@1": "&5[&4].van_cost.&2[]"
}
}
}
}
}
}
},
// use the first element of each inex array from above with the
// destinationName to generate the destination object.
// align indexes from all arrays to generate the transportationList
{
"operation": "shift",
"spec": {
"destination": {
"*": {
"destinationName": {
"*": {
"@": "destination[#2].destName",
"@(2,van_cost.&[0])": "destination[#2].vanFee"
}
},
"taxi_cost": {
"*": {
"@(2,destinationName[&])": "transportList.transportDestination[#2].name",
"@": "transportList.transportDestination[#2].transports[#].costs[]",
"@(2,van_cost.&[0])": "transportList.transportDestination[#2].transports[#].costs[]",
"@(2,subway_cost[&])": "transportList.transportDestination[#2].transports[#].costs[]",
"@(2,bus_cost[&])": "transportList.transportDestination[#2].transports[#].costs[]"
}
}
}
}
}
}
]