Skip to content Skip to sidebar Skip to footer

Lodash:getting New Array With Multiple Key-value Matches In Json

I have nessted JSON look like this [ {arrivalTime: '10:30 PM' availableSeats: 23 boardingPoints: [{id: '3882' location: 'abc' time: '02:30PM'},{id: '3882'

Solution 1:

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);
});

Post a Comment for "Lodash:getting New Array With Multiple Key-value Matches In Json"