0

我的原始数据如下所示:

Array [
  Array [
   Object { 
      "Date": "Sun Mar 24 14:08:34 2021",
      "customer_name": "Test name 1",
      "customer_paid": 66, 
    },
  ],
  Array [
    Object { 
      "Date": "Sun Mar 21 14:08:34 2021",
      "customer_name": "Test name 2",
      "customer_paid": 46, 
    },
  ],
]

我试着让它看起来像这样:

CvertData ={{
    'Sun Mar 21 14:08:34 2021': [{Client: 'Test name 1' , amount : 66}],
    'Sun Mar 21 14:08:34 2021': [{Client: 'Test name 2', amount: 46}]
}} 

我确实使用了这段代码,但我不知道问题出在哪里:

const items = [];

for (let element of OriginalData) {
    var Elmdate= element['Date']
    items[Elmdate].push({
      Client: element['customer_name'],
      amount: element['customer_paid']
    });
}
4

1 回答 1

0

您有一个数组数组,而您要做的只是迭代外部数组并尝试使用它访问内部数组对象。

您还试图push在未定义的值上调用辅助方法。

我认为这应该可以解决您面临的问题-

const items = {};

for (let elements of OriginalData) {
  for (let element of elements) {
    var Elmdate = element['Date']
    let obj = {
      Client: element['customer_name'],
      amount: element['customer_paid']
    }
    if (items[Elmdate]) {
      items[Elmdate].push(obj);
    } else {
      items[Elmdate] = [obj]
    }
  }
}
于 2021-04-26T09:27:00.413 回答