Lodash:Getting 在 json 中具有多个键值匹配的新数组
Lodash:Getting new array with multiple key-value matches in json
我嵌套了 JSON 看起来像这样
[
{arrivalTime: "10:30 PM"
availableSeats: 23
boardingPoints: [{id: "3882"
location: "abc"
time: "02:30PM"},{id: "3882"
location: "xyz"
time: "02:30PM"}]
busType: "Scania Metrolink"
operatorName:"sham"
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: [{id: "3882"
location: "dex"
time: "02:30PM"},{id: "3882"
location: "afg"
time: "02:30PM"}]
},
{arrivalTime: "10:30 PM"
availableSeats: 23
boardingPoints: [{id: "3882"
location: "def"
time: "02:30PM"},{id: "3882"
location: "jkl"
time: "02:30PM"}]
busType: "Scania "
operatorName:"manu"
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: [{id: "3882"
location: "ccd"
time: "02:30PM"},{id: "3882"
location: "eef"
time: "02:30PM"}]
}
]
由此我想获得与这些 key
值匹配的新数组。
这是钥匙。
1.BoardingPoints.
2.DroppingPoints.
3.busType.
4.OperatorName.
例如:
如果这样输入
BoardingPoints=['abc']
DroppingPoints=['ccd','eef']
busType=['Scania Metrolink']
,
OperatorName=['manu']
应该returns这两行
{arrivalTime: "10:30 PM" availableSeats: 23 boardingPoints: [{id:
"3882" location: "abc" time: "02:30PM"},{id: "3882" location:
"xyz" time: "02:30PM"}] busType: "Scania Metrolink"
operatorName:"sham" commPCT: 8 departureTime: "1:15 PM"
droppingPoints: [{id: "3882" location: "dex" time: "02:30PM"},{id:
"3882" location: "afg" time: "02:30PM"}] },
{arrivalTime: "10:30 PM"
availableSeats: 23 boardingPoints: [{id: "3882" location: "def" time:
"02:30PM"},{id: "3882" location: "jkl" time: "02:30PM"}] busType:
"Scania " operatorName:"manu" commPCT: 8 departureTime: "1:15 PM"
droppingPoints: [{id: "3882" location: "ccd" time: "02:30PM"},{id:
"3882" location: "eef" time: "02:30PM"}] } ]
备注
每个输入都作为数组传递,因为我需要匹配键中的多个值。
从预期的结果来看,您似乎正在寻找与 4 个变量中的任何一个匹配的对象。这是将匹配它们的过滤器:
var bpLocations = ['abc'];
var dpLocations = ['ccdll', 'eef'];
var busTypes = ['Scania Metrolink'];
var operatorNames = ['manu'];
var result = _.filter(inputArray, function(obj) {
return _(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0
|| _(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0
|| _.includes(busTypes, obj.busType)
|| _.includes(operatorNames, obj.operatorName);
});
我嵌套了 JSON 看起来像这样
[
{arrivalTime: "10:30 PM"
availableSeats: 23
boardingPoints: [{id: "3882"
location: "abc"
time: "02:30PM"},{id: "3882"
location: "xyz"
time: "02:30PM"}]
busType: "Scania Metrolink"
operatorName:"sham"
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: [{id: "3882"
location: "dex"
time: "02:30PM"},{id: "3882"
location: "afg"
time: "02:30PM"}]
},
{arrivalTime: "10:30 PM"
availableSeats: 23
boardingPoints: [{id: "3882"
location: "def"
time: "02:30PM"},{id: "3882"
location: "jkl"
time: "02:30PM"}]
busType: "Scania "
operatorName:"manu"
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: [{id: "3882"
location: "ccd"
time: "02:30PM"},{id: "3882"
location: "eef"
time: "02:30PM"}]
}
]
由此我想获得与这些 key
值匹配的新数组。
这是钥匙。
1.BoardingPoints.
2.DroppingPoints.
3.busType.
4.OperatorName.
例如: 如果这样输入
BoardingPoints=['abc']
DroppingPoints=['ccd','eef']
busType=['Scania Metrolink']
,
OperatorName=['manu']
应该returns这两行
{arrivalTime: "10:30 PM" availableSeats: 23 boardingPoints: [{id: "3882" location: "abc" time: "02:30PM"},{id: "3882" location: "xyz" time: "02:30PM"}] busType: "Scania Metrolink" operatorName:"sham" commPCT: 8 departureTime: "1:15 PM" droppingPoints: [{id: "3882" location: "dex" time: "02:30PM"},{id: "3882" location: "afg" time: "02:30PM"}] },
{arrivalTime: "10:30 PM" availableSeats: 23 boardingPoints: [{id: "3882" location: "def" time: "02:30PM"},{id: "3882" location: "jkl" time: "02:30PM"}] busType: "Scania " operatorName:"manu" commPCT: 8 departureTime: "1:15 PM" droppingPoints: [{id: "3882" location: "ccd" time: "02:30PM"},{id: "3882" location: "eef" time: "02:30PM"}] } ]
备注
每个输入都作为数组传递,因为我需要匹配键中的多个值。
从预期的结果来看,您似乎正在寻找与 4 个变量中的任何一个匹配的对象。这是将匹配它们的过滤器:
var bpLocations = ['abc'];
var dpLocations = ['ccdll', 'eef'];
var busTypes = ['Scania Metrolink'];
var operatorNames = ['manu'];
var result = _.filter(inputArray, function(obj) {
return _(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0
|| _(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0
|| _.includes(busTypes, obj.busType)
|| _.includes(operatorNames, obj.operatorName);
});