React native push is not a function

360 Views Asked by At

My original data looks like this :

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, 
    },
  ],
]

I try to make it look like this :

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}]
}} 

I did use this code but i don't know where is the problem :

const items = [];

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

There are 1 best solutions below

0
On

You have an array of array, and what you are trying to do is only iterate the outer array and trying to access the inner array object with that.

Also you are trying to call the push helper method on an undefined value.

I think this should solve the issue you are facing -

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]
    }
  }
}