Lodash:在json中获取具有多个键值匹配的新数组 [英] Lodash:Getting new array with multiple key-value matches in json
问题描述
我已经将JSON变成了这样
I have nessted JSON look like this
[
{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
值匹配的新数组.
这是钥匙.
From this i want to get new array that matches the these key
values.
Here is the keys.
1.BoardingPoints.
1.BoardingPoints.
2.DroppingPoints.
2.DroppingPoints.
3.busType.
3.busType.
4.OperatorName.
4.OperatorName.
例如: 如果输入是这样
Eg: if the input like this
BoardingPoints=['abc']
DroppingPoints=['ccd','eef']
busType=['Scania Metrolink']
,
OperatorName=['manu']
它应该返回这两行
{到达时间:晚上10:30"可容纳座位:23登机点:[{id: "3882"位置:" abc "时间:"02:30 PM"},{id:"3882"位置: "xyz"时间:"02:30 PM"}] busType:" Scania Metrolink " operatorName:"sham" commPCT:8出发时间:"1:15 PM" droppingPoints:[{id:"3882"位置:"dex"时间:"02:30 PM"},{id: "3882"位置:"afg"时间:"02:30 PM"}]},
{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"}] },
{到达时间:晚上10:30" 可用座位:23登机点:[{id:"3882"位置:"def"时间: " 02:30 PM},{id:" 3882位置:" jkl时间:" 02:30 PM}] busType: 斯堪尼亚"操作员姓名:"手册" commPCT:8出发时间:"1:15 PM" droppingPoints:[{id:"3882"位置:" ccd "时间:"02:30 PM"},{id: "3882"位置:" ef "时间:"02:30 PM"}]}]
{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"}] } ]
注意
每个输入都作为数组传递,因为我需要匹配键中的多个值.
Each input is passed as an array because i need to match multiple values in the keys.
推荐答案
从预期结果来看,您似乎正在寻找与4个变量中的任何一个都匹配的对象.这是与它们匹配的过滤器:
From the expected result, it looks like you are looking for the objects that match any of the 4 variables. Here is the filter that will match them:
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);
});
这篇关于Lodash:在json中获取具有多个键值匹配的新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!