javascript - 使用 RxJS/TypeScript/JavaScript 将行转换为列

标签 javascript angular typescript rxjs

我有以下表格数据:

<表类=“s-表”> <标题> 日期 值 <正文> 2000年1月1日 1 2000年1月2日 2 2001年1月1日 2 2002年1月1日 1.5 2002年1月2日 1.6
[{date: "01/01/2000", value: "1"},{date: "01/02/2000", value: "2"},{date: "01/01/2001", value: "2"},{date: "01/01/2002", value: "1.5"},{date: "01/02/2002", value: "1.6"}]

我想将其转换为列:

<表类=“s-表”> <标题> 年 一月 二月 <正文> 2000 1 2 2001 2 2002 1.5 1.6
[{Year: "2000", Jan: "1", Feb: "2"},
{Year: "2001", Jan: "", Feb: "2"},
{Year: "2002", Jan: "1.5", Feb: "1.6"}]

如何在 Angular 中使用 RxJS/TypeScript/JavaScipt 更改它?

谢谢。

最佳答案

请参阅下面使用reduce的方法

const initial = [{date: "01/01/2000", value: "1"},{date: "01/02/2000", value: "2"},{date: "01/01/2001", value: "2"},{date: "01/01/2002", value: "1.5"},{date: "01/02/2002", value: "1.6"}]

const allMonths = initial.reduce((prev, next) => {
  const date = next.date.substr(6,4) + '/' + next.date.substr(3,2)
  const month =  new Date(date).toLocaleString('default', { month: 'short' });
  return {...prev, [month]: ''}
}, {})

const temp = initial.reduce(
  (prev, next) => {
    const date = next.date.substr(6,4) + '/' + next.date.substr(3,2)
    const month =  new Date(date).toLocaleString('default', { month: 'short' });
    
    const Year = new Date(date).getFullYear()
    let prevYearVal = prev[Year]
    if(!prevYearVal) { prevYearVal = {Year,...allMonths} ;}
    return {...prev, [Year]: {...prevYearVal,Year, [month]: next.value}}
    
    return prev
  },
  {}
)
const final = Object.values(temp)
console.log(final)

关于javascript - 使用 RxJS/TypeScript/JavaScript 将行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66320817/

相关文章:

javascript - 提交后下载文件 联系表 7 WordPress

javascript - 如何处理ag grid( Angular )中的对象的嵌套数组

angular - 减少从时间戳中获取/显示的数据

typescript - 函数返回对象类型

javascript - 为什么 CSS 中的这个 SVG 片段标识符在操作 onclick 时不会呈现?

php - 在isset($_GET ['post_id' ]) === true)之后,需要调用Javascript将<div>可见性变为隐藏

javascript - 防止为未打开的扩展面板呈现内部 html

angular - 使用 Testbed 的 angular2 测试组件出错

validation - angular2 自定义模板验证器具有过时值

javascript - JS为动态声明的元素委托(delegate)唯一事件(one())