【发布时间】:2016-06-01 20:26:42
【问题描述】:
我有一个像这样的嵌套 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"
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: null
},
{arrivalTime: "10:30 PM"
availableSeats: 23
boardingPoints: [{id: "3882"
location: "def"
time: "02:30PM"},{id: "3882"
location: "jkl"
time: "02:30PM"}]
busType: "Scania "
commPCT: 8
departureTime: "1:15 PM"
droppingPoints: null
}
]
从此我想获得与条件匹配的新数组。 在这里
1.在boardingPoints对象中获取只有用户指定location的新数组。
eg-a:假设位置值为xyz,它将只返回带有
仅在 boardingPoints 对象中包含位置 xyz。
输出
{到达时间:“晚上 10:30” 可用座位:23 登机点:[{id:“3882” 位置:“ABC” 时间:“02:30PM”},{id:“3882” 位置:“xyz” 时间:“02:30PM”}] busType:“斯堪尼亚 Metrolink” comPCT: 8 出发时间:“下午 1 点 15 分” 丢弃点:空 }
eg-b:假设位置值为xyz 和def,它应该只返回包含上述两个位置的JSON,仅在boardingPoints 对象中。
输出
{到达时间:“晚上 10:30” 可用座位:23 登机点:[{id:“3882” 位置:“ABC” 时间:“02:30PM”},{id:“3882” 位置:“xyz” 时间:“02:30PM”}] busType:“斯堪尼亚 Metrolink” comPCT: 8 出发时间:“下午 1 点 15 分” 丢弃点:空 }, {到达时间:“晚上 10:30” 可用座位:23 登机点:[{id:“3882” 位置:“def” 时间:“02:30PM”},{id:“3882” 位置:“jkl” 时间:“02:30PM”}] 总线类型:“斯堪尼亚” comPCT: 8 出发时间:“下午 1 点 15 分” 丢弃点:空 }
我知道这可以使用lodash 来实现,但我不知道该怎么做
目前我只知道lodash 中的匹配项,但我不知道如何在我的情况下使用它。
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, _.matches({ 'age': 40}));
// → [{ 'user': 'fred', 'age': 40, 'active': false }]
是否可以在javascript中使用本机方法?
【问题讨论】:
标签: php html arrays json lodash