最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Lodash从对象中过滤出流氓物品

IT培训 admin 8浏览 0评论

Lodash从对象中过滤出流氓物品

我正在使用lodash map和uniqby从json文件events.json创建一个对象。

我很少得到一个除“英国”以外的国家的活动。我在结果中不需要它们,我将如何“不映射”不在“英国”中的事件。谢谢

var _ = require('lodash');
var data = require('./events.json')

var newEventList = _.uniqBy(data.events,'id').map(events => ({
    id: events.id ,
    name: events.name ,
    venue: events.venue.name ,
    address: events.place.location.street + " " + events.place.location.city + " " + events.place.location.zip + " " + events.place.location.country
}));

我查看了_.filter,但不确定在map函数中使用它的位置。

回答如下:

您可以在地图之前或之后使用过滤器。但在地图之前它会更有用。

var newEventList = _.uniqBy(data.events,'id').filter(event => event.place.location.country === 'United Kingdom').map(event => ({
    id: event.id ,
    name: event.name ,
    venue: event.venue.name ,
    address: event.place.location.street + " " + event.place.location.city + " " + event.place.location.zip + " " + event.place.location.country
}));

但问题是为什么你需要idq的id?通常id是一个应该是uniq的属性

Lodash从对象中过滤出流氓物品

我正在使用lodash map和uniqby从json文件events.json创建一个对象。

我很少得到一个除“英国”以外的国家的活动。我在结果中不需要它们,我将如何“不映射”不在“英国”中的事件。谢谢

var _ = require('lodash');
var data = require('./events.json')

var newEventList = _.uniqBy(data.events,'id').map(events => ({
    id: events.id ,
    name: events.name ,
    venue: events.venue.name ,
    address: events.place.location.street + " " + events.place.location.city + " " + events.place.location.zip + " " + events.place.location.country
}));

我查看了_.filter,但不确定在map函数中使用它的位置。

回答如下:

您可以在地图之前或之后使用过滤器。但在地图之前它会更有用。

var newEventList = _.uniqBy(data.events,'id').filter(event => event.place.location.country === 'United Kingdom').map(event => ({
    id: event.id ,
    name: event.name ,
    venue: event.venue.name ,
    address: event.place.location.street + " " + event.place.location.city + " " + event.place.location.zip + " " + event.place.location.country
}));

但问题是为什么你需要idq的id?通常id是一个应该是uniq的属性

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论