简体   繁体   中英

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.

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. 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 .

 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 . If you get a repeating course, then compare the DateStart and store the prior value. Once you get all the unique course in an object accumulator, get all values using 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 }))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM