请考虑以下数据:
{
post_views:[
{
id:1,
post_id:5,
ip_address:"xxx",
created_at:"2016-08-08T22:22:45+0000",
updated_at:"2016-08-08T22:22:45+0000"
},
{
id:2,
post_id:5,
ip_address:"yyy",
created_at:"2016-08-08T22:23:00+0000",
updated_at:"2016-08-08T22:23:00+0000"
},
...
]
}
如何获得匹配的所有元素:2016-08-08 ??
您尝试了什么?
Lodashes,过滤器:
let datesThatMatch = filter(this.state.data.post_views, (views) => {
return moment(views.created_at).format('MMMM Do YYYY') === moment(views.created_at).format('MMMM Do YYYY')
});
console.log(datesThatMatch);
我不关心他们创建的时间,就像他们在比赛中创建的那一天一样。
帮助?
我刚刚意识到我也是这样做的全部都是错的,因为虽然我只向你展示了一部分数据,但数组中有数百个这些对象(如果不是数千个),目标就是说,确定2016-08-08的16场比赛,x日期的50场比赛,y场的14场比赛,这增加了这个过滤器的复杂性。
答案 0 :(得分:2)
我认为groupBy是基于您的更新的更好选择:
var data = {
post_views:[
{
id:1,
post_id:5,
ip_address:"xxx",
created_at:"2016-08-08T22:22:45+0000",
updated_at:"2016-08-08T22:22:45+0000"
},
{
id:2,
post_id:5,
ip_address:"yyy",
created_at:"2016-08-08T22:23:00+0000",
updated_at:"2016-08-08T22:23:00+0000"
},
{
id:3,
post_id:5,
ip_address:"zzz",
created_at:"2016-08-12T20:15:00+0000",
updated_at:"2016-08-12T21:20:00+0000"
},
]
}
let groupedDatesThatMatch = _.groupBy(data.post_views, function(views) {
return moment(views.created_at).format('MMMM Do YYYY')
});
console.log(groupedDatesThatMatch);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.2/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
你从另一端得到一个数组对象
答案 1 :(得分:0)
您可以构建“反向查找表”(即指示每个键出现的位置) 以下是示例演示;
var a = [1,1,2,3,4,5,6,6,7];
var reverseIndex = new Map();
a.every((cVal, i, a) => {
var existing = reverseIndex.has(cVal) ? reverseIndex.get(cVal) : [];
existing.push(i);
reverseIndex.set(cVal, existing);
return true;
});
console.log(reverseIndex);
答案 2 :(得分:0)
怎么样......
let post_views = [
{
id:1,
post_id:5,
ip_address:"xxx",
created_at:"2016-08-08T22:22:45+0000",
updated_at:"2016-08-08T22:22:45+0000"
},
{
id:2,
post_id:5,
ip_address:"yyy",
created_at:"2016-08-08T22:23:00+0000",
updated_at:"2016-08-08T22:23:00+0000"
}
]
let looking_for = new RegExp('2016-08-08')
let result = post_views.filter(view => looking_for.test(view.created_at) || looking_for.test(view.updated_at) )
let summary = result.reduce((prev,view) => {
let created_at = view.created_at.split('T')[0];
let updated_at = view.updated_at.split('T')[0];
prev.created_at[created_at] = prev.created_at[created_at] || 0;
prev.updated_at[updated_at] = prev.updated_at[updated_at] || 0;
prev.created_at[created_at]++;
prev.updated_at[updated_at]++;
return prev;
},{ created_at: {}, updated_at: {} })
console.log(result);
console.log(summary);
答案 3 :(得分:0)
经过一番探索之后,我设法提出了一个使用dateformat的解决方案,并允许您轻松自定义日期。这不使用库和直接ES6。如果您运行代码段,则会看到您的日期匹配并存储。
const arr = {
post_views:[
{
id:1,
post_id:5,
ip_address:"xxx",
created_at:"2016-08-08T22:22:45+0000",
updated_at:"2016-08-08T22:22:45+0000"
},
{
id:2,
post_id:5,
ip_address:"yyy",
created_at:"2016-08-08T22:23:00+0000",
updated_at:"2016-08-08T22:23:00+0000"
},
{
id:3,
post_id:5,
ip_address:"yyy",
created_at:"2016-08-09T22:23:00+0000",
updated_at:"2016-08-08T22:23:00+0000"
},
]
}
const filterDate = new Date(2016, 08, 08);
function parseDate(date) {
const dateString = date.split("T")[0].split('-'); //Get just the date portion, and then split that into an array.
const newDate = new Date(Number(dateString[0]), Number(dateString[1]), Number(dateString[2])) //Make the new date object
return newDate;
}
let myMatches = arr.post_views.filter(thisDate => parseDate(thisDate.created_at).getTime() == filterDate.getTime());
console.log(myMatches);