Lodash:使用JSON中的多个匹配获取新数组

时间:2016-02-20 04:56:46

标签: php html arrays json lodash

我有一个嵌套的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
}    
]

从此我想得到一个与condtion相匹配的新数组。 这是它

1.在location对象中仅使用指定用户boardingPoints的新数组。

例如-a:假设位置值为xyz,它将仅返回JSON  仅包含xyz对象中的位置boardingPoints

输出

{到达时间:"晚上10:30"  availableSeats:23 boardingPoints:[{id:" 3882" 位置:" abc" 时间:" 02:30 PM"},{id:" 3882" 位置:" xyz " 时间:" 02:30 PM"}] busType:" Scania Metrolink" commPCT:8 出发时间:"下午1:15" droppingPoints:null }

例如-b:假设位置值为xyzdef它应仅返回仅包含boardingPoints对象中上述两个位置的JSON。

输出

{到达时间:"晚上10:30"  availableSeats:23 boardingPoints:[{id:" 3882" 位置:" abc" 时间:" 02:30 PM"},{id:" 3882" 位置:" xyz " 时间:" 02:30 PM"}] busType:" Scania Metrolink" commPCT:8 出发时间:"下午1:15" droppingPoints:null }, {到达时间:"晚上10:30"  availableSeats:23 boardingPoints:[{id:" 3882" 位置:" def " 时间:" 02:30 PM"},{id:" 3882" 位置:" jkl" 时间:" 02:30 PM"}] busType:"斯堪尼亚" commPCT:8 出发时间:"下午1:15" droppingPoints:null }

我知道这可以使用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中的原生方法?

2 个答案:

答案 0 :(得分:3)

您可以使用一系列带有一些逻辑的lodash调用来获得预期的结果。像这样:

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'];

结果将是你的eg-b

解决方案的另一种方法是使用intersection()链接调用:

var f = _.filter(result, function(obj) {
  return _.chain(obj.boardingPoints).map('location').intersection(locations).value().length > 0;
});

答案 1 :(得分:0)

var result = _.filter(yourArray, function(item) {
    return _.size(_.intersection(['xyz', 'def'], _.map(item.boardingPoints, 'location')));
});

https://jsfiddle.net/qeaabov9/2/