问题
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
}
]
From this i want to get new array that matches a condtion. Here is it
1.get the new array with only the user specified location in boardingPoints object.
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.
output
{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 }
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.
output
{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 }
I know this may be implemented using lodash but i don't know how to do it
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 }]
Is it possible with native method in javascript?
回答1:
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
when
locations = ['xyz', 'def'];
the result will be your eg-b
Another approach to the solution is to chain the calls with intersection():
var f = _.filter(result, function(obj) {
return _.chain(obj.boardingPoints).map('location').intersection(locations).value().length > 0;
});
回答2:
var result = _.filter(yourArray, function(item) {
return _.size(_.intersection(['xyz', 'def'], _.map(item.boardingPoints, 'location')));
});
https://jsfiddle.net/qeaabov9/2/
来源:https://stackoverflow.com/questions/35519293/lodashgetting-new-array-with-mulitple-matches-in-json