最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin11浏览0评论

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

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. 暂无评论