【发布时间】:2020-05-28 18:56:31
【问题描述】:
我在date 上使用[...new Map],它应该只传递date 的唯一值。但是console.log(uniqueObjects); 不会打印出唯一的日期值。我在[...new Map(date.map(item => [item.date, item])).values()]; 做错了一些事情,它没有传递唯一的日期值。有没有办法使用class='filter'打印唯一日期值?
脚本
new Vue({
el: "#app",
data: {
todos: [
{ id:"1", date:"2020-05-28T18:30:00.000Z", peopleName: "David" },
{ id:"2", date:"2020-05-28T18:30:00.000Z", peopleName: "Jack" },
{ id:"3", date:"2020-05-28T20:00:00.000Z", peopleName: "John" },
{ id:"4", date:"2020-05-28T20:00:00.000Z", peopleName: "Thomas" },
{ id:"5", date:"2020-05-28T20:00:00.000Z", peopleName: "Will" },
{ id:"6", date:"2020-05-28T21:30:00.000Z", peopleName: "Hary" },
]
},
computed:{
filter: {
moment: function (date) {
console.log("its not printing");
/** return moment(date).format('h:mm A') **/
console.log(date);
const uniqueObjects = [...new Map(date.map(item => [item.date, item])).values()];
/** i am using new Map to remove the same value for date but it's not working **/
console.log(uniqueObjects);
}
}
},
methods: {
toggle: function(todo){
todo.done = !todo.done
}
}
})
查看
<div id="app">
<div v-for="reservation in todos" v-bind:key="reservation.id">
<p class="filter">{{ reservation.date | moment }}</p>
<p>{{reservation.peopleName}}</p>
</div>
</div>
下面是jsfidddle上的代码链接
【问题讨论】: