简体   繁体   English

使用map,reduce或filter按属性获取唯一值数组javascript

[英]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.

相关问题 在JavaScript中使用带有reduce的map来过滤数组中的对象 - Using map with reduce in javascript to filter objects in an array 如何在使用 Javascript.reduce 而不是.filter().map() 构建新的对象数组时添加属性? - How to add a property when building a new array of objects using Javascript .reduce, as opposed to .filter().map()? 使用 map 从嵌套数组打印,在 javascript 中减少和过滤 - Print from nested array using map, reduce and filter in javascript 使用 map、filter、reduce 将对象数组转换为具有额外属性的对象数组 - Transform array of objects into array of objects with an extra property with map, filter, reduce Javascript map然后过滤唯一的数组项 - Javascript map then filter unique array items 过滤对象数组并使用值数组获取其属性 - Filter an array of objects and get its property by using an array of values 使用 Javascript Map 和 Reduce 操作对象数组 - Using Javascript Map and Reduce to manipulate Array of Objects 使用地图遍历对象。 在javascript中过滤,缩小或Object.keys() - Traverse array of objects using map. filter, reduce or Object.keys() in javascript 如何使用map(),reduce()和filter()简化javascript嵌套数组? - How to simplify javascript nested array with map(), reduce(), and filter()? 使用map和reduce来获得对象数组 - Using map and reduce to get array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM