【问题标题】:how to sort an array based on multiple fields in javascript如何根据javascript中的多个字段对数组进行排序
【发布时间】:2019-10-28 22:34:40
【问题描述】:

我有一个数组对象

events = [
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
   },
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
    },
    {
      year: "2019",
      month: "July",
      title: "title",
      desc: "desc"
    },
    {
      year: "2018",
      month: "June",
      title: "title",
      desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
      }
    ]

如何对数组进行排序以显示相似的年份和月份,我需要在反应组件中使用它,例如一行将显示 2019 年 6 月,另一行显示 2019 年 7 月,然后是 2018 年 6 月,依此类推,

任何帮助将不胜感激

【问题讨论】:

  • 我认为 json 中的 Typo 错误
  • 相似的年份和月份”是什么意思。你有想要的结果的例子吗?
  • @NinaScholz,我需要将 2019 年,6 月显示在一行,2019 年 7 月在另一行,2018 年 6 月在另一行等,react 组件完成,我只是无法弄清楚如何对数组进行排序以获得所需的值。

标签: javascript ecmascript-6


【解决方案1】:

您需要为.sort 提供一个自定义比较器函数,以便使用月份和年份进行排序。

const events = [
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
   },
   {
     year: "2019",
     month: "June",
     title: "title",
     desc: "desc"
    },
    {
      year: "2019",
      month: "July",
      title: "title",
      desc: "desc"
    },
    {
      year: "2018",
      month: "June",
      title: "title",
      desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
     },
     {
       year: "2018",
       month: "March",
       title: "title",
       desc: "desc"
      }
];

// Custom comparator function
function sortByMonthYear(a, b) {
	const keyA = `${a.month} ${a.year}`;
	const keyB = `${b.month} ${b.year}`;
	
	if (keyA.localeCompare(keyB)) {
		return -1;
	}
	else if (keyA === keyB) {
		return 0;
	}
	return 1;
}


const grouping = events.sort(sortByMonthYear);

console.log(grouping);

更新:

您还可以使用Array#prototype#reduce 对数据进行分组

const events = [{
    year: "2019",
    month: "June",
    title: "title",
    desc: "desc"
  },
  {
    year: "2019",
    month: "June",
    title: "title",
    desc: "desc"
  },
  {
    year: "2019",
    month: "July",
    title: "title",
    desc: "desc"
  },
  {
    year: "2018",
    month: "June",
    title: "title",
    desc: "desc"
  },
  {
    year: "2018",
    month: "March",
    title: "title",
    desc: "desc"
  },
  {
    year: "2018",
    month: "March",
    title: "title",
    desc: "desc"
  }
];

const grouping = events.reduce((acc, curr) => {
  const key = `${curr.month} ${curr.year}`;
  if (!acc[key]) {
    acc[key] = [curr];
  } else {
    acc[key].push(curr);
  }
  return acc;
}, {});

console.log(grouping);

【讨论】:

  • 感谢您的回答,实际上您之前发布的答案正是我所需要的
  • @akano1 您能否提供一些您的问题所需的示例输出
【解决方案2】:

通过它们创建对一年中所有月份和flatMap 的引用,在flatMap 中针对原始事件运行过滤器,以仅返回与迭代中的当前月份共享相同month 的任何事件。 完成此操作后,您可以在列表中运行 sort 以确保事件按 year 排序(从小到大)

const MOY = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const events = [
 {year: "2019",month: "June",title: "title",desc: "desc"},
 {year: "2019",month: "June",title: "title",desc: "desc"},
 {year: "2019",month: "July",title: "title",desc: "desc"},
 {year: "2018",month: "June",title: "title",desc: "desc"},
 {year: "2018",month: "March",title: "title",desc: "desc"},
 {year: "2018",month: "March",title: "title",desc: "desc"}
]
    
  
const sortedByMonth = MOY.flatMap(m => events.filter(({month}) => m === month))
const sortedByYear = sortedByMonth.sort((eA, eB) => eA.year < eB.year) 
  
console.log(sortedByMonth)
console.log(sortedByYear)

或者,如果您想按月分块,但按年排序,那么就这样做

const MOY = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const events = [
 {year: "2019",month: "June",title: "title",desc: "desc"},
 {year: "2019",month: "June",title: "title",desc: "desc"},
 {year: "2019",month: "July",title: "title",desc: "desc"},
 {year: "2018",month: "June",title: "title",desc: "desc"},
 {year: "2018",month: "March",title: "title",desc: "desc"},
 {year: "2018",month: "March",title: "title",desc: "desc"}
] 
  
const sorted = MOY.flatMap(m => (
  events
  .filter(({month}) => m === month)
  .sort((eA, eB) => eA.year < eB.year)
))
  
console.log(sorted)

【讨论】:

    猜你喜欢
    • 2013-10-16
    • 2022-11-18
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多