[英]get unique values array javascript by property using map, reduce or filter
I have this array and want to get only unique Courses and more recent DateStart.我有这个数组,只想获得独特的课程和更新的 DateStart。
const classes = [{
Course: {id: 1, Name: 'JS'},
Name: 'JS Morning',DateStart: "2018/09/01"
}, {
Course: {id: 1, Name: 'JS'},
Name: 'JS Afternoon',DateStart: "2018/10/15"
}, {
Course: {id: 1, Name: 'JS'},
Name: 'JS Night',DateStart: "2018/10/01"
}, {
Course: {id: 2, Name: 'Jquery'},
Name: 'JQ Morning',DateStart: "2018/10/01"
}, {
Course: {id: 2, Name: 'Jquery'},
Name: 'JQ Night',DateStart: "2018/09/15"
}];
Per example, my result array should be:例如,我的结果数组应该是:
const results = [{
DataStart:"2018/09/01",Course{id: 1, Name: "JS"}
},{
DataStart:"2018/09/15",Course{id: 2, Name: "Jquery"}
I know to do this using foreach, but I want to use MAP or REDUCE or FILTER.我知道使用 foreach 来执行此操作,但我想使用 MAP 或 REDUCE 或 FILTER。 Could anybody help me?有人可以帮我吗?
Thanks!!!谢谢!!! Alex亚历克斯
We could do first a sort()
by date, in this case is easier comparing the dates as numbers, replacing the /
like 20180901
, once we have the sorted array we could do a filter()
to keep just the first record of a Course.Name
.我们可以20180901
日期进行sort()
,在这种情况下更容易将日期比较为数字,替换/
像20180901
,一旦我们有了排序的数组,我们就可以执行filter()
以仅保留Course.Name
的第一条记录Course.Name
。
var classes = [{ Course: {id: 1, Name: 'JS'}, Name: 'JS Morning',DateStart: "2018/09/01" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Afternoon',DateStart: "2018/10/15" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Night',DateStart: "2018/10/01" }, { Course: {id: 2, Name: 'Jquery'}, Name: 'JQ Morning',DateStart: "2018/10/01" }, { Course: {id: 2, Name: 'Jquery'}, Name: 'JQ Night',DateStart: "2018/09/15" }]; var diff = {} results = classes.sort((a,b)=>{ return a.DateStart.replace(/\\//g, "") - b.DateStart.replace(/\\//g, ""); }).filter(a=>{ delete a.Name if(a.Course.Name in diff){ return false; }else{ diff[a.Course.Name] = true; return true; } }) console.log(results)
var classes = [{ Course: {id: 1, Name: 'JS'}, Name: 'JS Morning',DateStart: "2018/09/01" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Afternoon',DateStart: "2018/09/01" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Night',DateStart: "2018/09/01" }, { Course: {id: 2, Name: 'Jquery'}, Name: 'JQ Morning',DateStart: "2018/09/15" }, { Course: {id: 2, Name: 'Jquery'}, Name: 'JQ Night',DateStart: "2018/09/15" }]; var result = classes.map(x => { return { DateStart: x.DateStart, Course: x.Course } }).filter((x,i,a)=> i == a.findIndex(y => y.DateStart == x.DateStart && y.Course.id == x.Course.id && y.Course.Name == x.Course.Name)); console.log(result);
You can use array#reduce
to get the unique courses using the id
.您可以使用array#reduce
来获取使用id
的独特课程。 If you get a repeating course, then compare the DateStart
and store the prior value.如果您获得重复课程,则比较DateStart
并存储先前值。 Once you get all the unique course in an object accumulator, get all values using Object.values()
.在对象累加器中获得所有独特的课程后,使用Object.values()
获得所有值。
const classes = [{ Course: {id: 1, Name: 'JS'}, Name: 'JS Morning',DateStart: "2018/09/01" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Afternoon',DateStart: "2018/10/15" }, { Course: {id: 1, Name: 'JS'}, Name: 'JS Night',DateStart: "2018/10/01" }, { Course:{id: 2, Name: 'Jquery'}, Name: 'JQ Morning',DateStart: "2018/10/01" }, { Course: {id: 2, Name: 'Jquery'}, Name: 'JQ Night',DateStart: "2018/09/15" }], result = Object.values(classes.reduce((r,{Course, DateStart}) => { if(r[Course.id]) { if(r[Course.id].DateStart > DateStart) r[Course.id] = {Course, DateStart}; } else r[Course.id] = {Course, DateStart}; return r; },{})); console.log(result);
您可以使用以下解决方案通过使用 map 的属性从数组中获取唯一值:
classes.map( c => ({ DataStart: c.DateStart, Course: c.Course }))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.