如何在特定日期获得JSON的唯一用户?

时间:2016-12-22 01:14:03

标签: javascript json

我有以下JSON超过1000行。 例如,我如何仅从第12个月开始列出所有用户?

我期待结果:发件人20,13和18.发件人4没有出现,因为它已经存在于第11个月

[{"recipient":1,"sender":4,"created_at":"2016-11-10","content":"Duis aliquam convallis nunc. Proin at turpis a pede posuere nonummy. Integer non velit. Donec diam neque, vestibulum eget, vulputate ut, ultrices vel, augue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec pharetra, magna vestibulum aliquet ultrices, erat tortor sollicitudin mi, sit amet lobortis sapien sapien non mi. Integer ac neque. Duis bibendum. Morbi non quam nec dui luctus rutrum. Nulla tellus. In sagittis dui vel nisl.","is_sender_user":true},
{"recipient":1,"sender":18,"created_at":"2016-12-10","content":"Proin eu mi.","is_sender_user":false},
{"recipient":1,"sender":4,"created_at":"2016-11-20","content":"In hac habitasse platea dictumst. Morbi vestibulum, velit id pretium iaculis, diam erat fermentum justo, nec condimentum neque sapien placerat ante. Nulla justo. Aliquam quis turpis eget elit sodales scelerisque. Mauris sit amet eros. Suspendisse accumsan tortor quis turpis. Sed ante. Vivamus tortor. Duis mattis egestas metus. Aenean fermentum.","is_sender_user":true},
{"recipient":1,"sender":20,"created_at":"2016-12-14","content":"Donec ut dolor.","is_sender_user":true},
{"recipient":1,"sender":13,"created_at":"2016-12-06","content":"Nulla mollis molestie lorem. Quisque ut erat. Curabitur gravida nisi at nibh.","is_sender_user":true}]

谢谢大家。

1 个答案:

答案 0 :(得分:1)

如果不考虑性能,这个答案可以解决您的问题吗?

// Get invalid Sender's IDs' list
var invalidSenders = array.filter(ele => !ele["created_at"].startsWith("2016-12")).map(ele => ele['sender']);
// Generate new list
var newArray = array.filter(ele => ele["created_at"].startsWith("2016-12") && !invalidSenders.includes(ele['sender']));
// Debug
newArray.forEach(obj => console.log(obj));