【问题标题】:Lodash:Getting new array with mulitple matches in JSONLodash:在 JSON 中获取具有多个匹配项的新数组
【发布时间】: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:假设位置值为xyzdef,它应该只返回包含上述两个位置的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


    【解决方案1】:

    您可以使用一系列带有一些逻辑的 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;
    });
    

    【讨论】:

    • 感谢您的回答
    • 还有一个问题,假设 json 是 {name:'aa',age:20}, {name:'bb',age:21}, {name:'cc',age:21}, {name:'dd',age:24},我想获取与多个名称匹配的行
    • 例如:如果名称是 aa 和 bb,则输出变为 {name:'aa',age:20}, {name:'bb',age:21}
    • 我怎样才能修改上面得到这个
    • 那个问题好像是这个问题:stackoverflow.com/questions/35496966/…
    【解决方案2】:
    var result = _.filter(yourArray, function(item) {
        return _.size(_.intersection(['xyz', 'def'], _.map(item.boardingPoints, 'location')));
    });
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多