Json 尝试使用力矩JS在对象上循环

Json 尝试使用力矩JS在对象上循环,json,angular,momentjs,lodash,Json,Angular,Momentjs,Lodash,我有一个部分,它从数组中提取数据并显示它,然后按月对它进行分组。为了让它显示日期对象,我必须从JSON文件中删除一个层 感觉好像我遗漏了一些非常小的东西,只需要对数据数组进行一个微小的更改就可以了。有人能指出我做错了什么吗 以下是显示数据的表格: <table class="table" *ngFor="let month of transactions | keyvalue"> <tr> <th>{{month.key}}</t

我有一个部分,它从数组中提取数据并显示它,然后按月对它进行分组。为了让它显示日期对象,我必须从JSON文件中删除一个层

感觉好像我遗漏了一些非常小的东西,只需要对数据数组进行一个微小的更改就可以了。有人能指出我做错了什么吗

以下是显示数据的表格:

<table class="table" *ngFor="let month of transactions | keyvalue">
    <tr>
        <th>{{month.key}}</th>
    </tr>
    <tr>
        <td>
            <table class="table" *ngFor="let customer of month.value">
                <tr>
                    <td>
                        {{customer}}
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
export class AppComponent {

  public transactions = {};
  // Sample Data
  customers = [
    {
      data: [
        {
          customer: {
            name: "John"
          },
          // transaction_date: "2017-04-18"
          transaction_date: "2019-9-22T13:56:11.971643+00:00"
        },
        {
          customer: {
            name: "Dick"
          },
          transaction_date: "2019-10-22T13:56:11.971643+00:00"
        },
        {
          customer: {
            name: "Harry"
          },
          transaction_date: "2019-7-22T13:56:11.971643+00:00"
        },
        {
          customer: {
            name: "John"
          },
          transaction_date: "2019-9-22T13:56:11.971643+00:00"
        }
      ]
    }
  ];

  constructor() {}

  ngOnInit() {
    const monthName = item =>
      moment(item.transaction_date, "YYYY-MM-DD").format("MMM");

    // Establish groupBy array
    this.transactions = _.chain(this.customers)
      .groupBy(monthName)
      .mapValues(items => _.map(items, "customer.name"))
      .value();

    const byMonth = _.chain(this.customers)
      .groupBy(monthName)
      .mapValues(items => _.map(items, "customer.name"))
      .value();
    console.log(byMonth);
    return byMonth;
    console.log(this.customers2);
  }
}
如果我以不同的方式格式化Json,它可以工作,但我需要它也能处理
data[]
数组

// Working Array
  customers2 = [
    {
      customer: {
        name: "John"
      },
      // transaction_date: "2017-04-18"
      transaction_date: "2019-9-22T13:56:11.971643+00:00"
    },
    {
      customer: {
        name: "Dick"
      },
      transaction_date: "2019-10-22T13:56:11.971643+00:00"
    },
    {
      customer: {
        name: "Harry"
      },
      transaction_date: "2019-7-22T13:56:11.971643+00:00"
    },
    {
      customer: {
        name: "John"
      },
      transaction_date: "2019-9-22T13:56:11.971643+00:00"
    }
  ];
试试这个:

 this.transactions = _.chain(this.customers[0].data)

 const byMonth = _.chain(this.customers[0].data)
工作

更新

您的
customers
成员变量是一个只有一个元素的数组

这就是添加
[0]
以访问它的原因

您只需访问这样一个数组中的第一个元素。数组名和索引值(从零开始)

再举几个例子:


customers=[];//loadsh函数之后,
事务的输出是什么?当代码不能按预期工作时会显示什么?这就成功了!你能解释一下你改变了什么吗?
[0].data
块是否强制数组在
data
上循环?