Lodash:获取新的阵列,在多张比赛JSON [英] Lodash:Getting new array with mulitple matches in JSON
问题描述
我有一个嵌套的JSON这个样子的。
I have a nested 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"
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
}
]
在此我希望得到一个condtion匹配新的数组。
下面是它
From this i want to get new array that matches a condtion. Here is it
1.get在指定只有用户位置
新阵列 boardingPoints
对象。
1.get the new array with only the user specified location
in boardingPoints
object.
例如-A:假设位置值为 XYZ
将只返回用JSON
包含位置 XYZ
仅在 boardingPoints
对象。
eg-a:Suppose the location value is xyz
it will return only the JSON with
that contains the location xyz
only in the boardingPoints
object.
输出
的 {arrivalTime:10:30 PM
availableSeats:23
boardingPoints:[{ID:3882
位置:ABC
时间:下午2点30},{ID:3882
位置: XYZ
时间:下午2点30}]
busType:斯堪尼亚林克
commPCT:8
departureTime:下午1:15
droppingPoints:空
} 的
例如-B:假设位置值为 XYZ
和 DEF
它应该只使用JSON返回包含上述两个位置仅在 boardingPoints
对象。
eg-b:Suppose location value is xyz
and def
it should returns only with JSON that contains the above two locations only in the boardingPoints
object.
输出
的 {arrivalTime:10:30 PM
availableSeats:23
boardingPoints:[{ID:3882
位置:ABC
时间:下午2点30},{ID:3882
位置: XYZ
时间:下午2点30}]
busType:斯堪尼亚林克
commPCT:8
departureTime:下午1:15
droppingPoints:空
},
{arrivalTime:10:30 PM
availableSeats:23
boardingPoints:[{ID:3882
位置: DEF
时间:下午2点30},{ID:3882
位置:JKL
时间:下午2点30}]
busType:斯堪尼亚
commPCT:8
departureTime:下午1:15
droppingPoints:空
} 的
我知道这可能使用实施 lodash
,但我不知道该怎么做。
I know this may be implemented using lodash
but i don't know how to do it
目前我知道的只有 lodash
的比赛,但我不知道我可以在我的情况下,使用此功能。
Currently i know about only matches in lodash
but i don't know how can i use this in my case.
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中的本地方法?
Is it possible with native method in javascript?
推荐答案
您可以使用一系列lodash的一些逻辑来得到预期的结果调用。事情是这样的:
You can use a series of lodash calls with some logic to get the expected results. Something like this:
var _ = require('lodash');
var result = [
{
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
}
];
var locations = ['xyz'];
var f = _.filter(result, function(obj) {
var value = _.map(obj.boardingPoints, 'location');
var i, len;
for (i = 0, len = locations.length; i < len; i++) {
if (_.indexOf(value, locations[i]) >= 0) {
return true;
}
}
return false;
});
console.log(f); // result is your eg-a
在
locations = ['xyz', 'def'];
的结果将是您如-B
the result will be your eg-b
到解决方案另一种方法是用连锁交叉口()的调用:
var f = _.filter(result, function(obj) {
return _.chain(obj.boardingPoints).map('location').intersection(locations).value().length > 0;
});
这篇关于Lodash:获取新的阵列,在多张比赛JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!