使用地图通过属性获得唯一值数组javascript,reduce或filter

时间:2018-09-02 21:01:47

标签: javascript ecmascript-6

我有这个数组,只想获取唯一的课程和最新的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"
}];

每个例子,我的结果数组应该是:

const results = [{
    DataStart:"2018/09/01",Course{id: 1, Name: "JS"}
},{
    DataStart:"2018/09/15",Course{id: 2, Name: "Jquery"}

我知道可以使用foreach来做到这一点,但是我想使用MAP或REDUCE或FILTER。有人可以帮我吗?

谢谢!!! 亚历克斯

4 个答案:

答案 0 :(得分:0)

我们可以先按日期执行sort(),在这种情况下,将日期与数字进行比较会更容易,一旦我们有了排序数组,就可以像/那样替换20180901 filter()仅保留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)

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

您可以使用array#reduce使用id获得独特的课程。如果您要重复学习,则比较DateStart并存储先前的值。在对象累加器中获得所有唯一的过程后,请使用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);

答案 3 :(得分:-1)

您可以使用以下解决方案,使用map通过属性从数组中获取唯一值:

classes.map( c => ({ DataStart: c.DateStart, Course: c.Course }))