I am very new to ReactJS. I have an array of object coming from API like this:
transact = [
{
"id": 111,
"trans_id": "NP2209200435",
"order_id": "POWERRw8",
"full_name": "Customer/INSTANT",
"narration": "2035011W",
"amount": "10.0000",
"email": "[email protected]",
"order_time": "2022-09-20 16:23:49",
"merchant_id": "MID629dcf88a4537",
"bank_id": null,
"gateway": "N/A",
"transaction_status": "Pending",
"card_scheme": null,
"attempts": 0,
"review_status": null
},
{
"id": 111,
"trans_id": "NP2209200435",
"order_id": "POWERRw8",
"full_name": "Customer/INSTANT",
"narration": "2035011W",
"amount": "10.0000",
"email": "[email protected]",
"order_time": "2022-09-20 16:23:49",
"merchant_id": "MID629dcf88a4537",
"bank_id": null,
"gateway": "N/A",
"transaction_status": "Pending",
"card_scheme": null,
"attempts": 0,
"review_status": null
},
{
"id": 111,
"trans_id": "NP2209200435",
"order_id": "POWERRw8",
"full_name": "Customer/INSTANT",
"narration": "2035011W",
"amount": "10.0000",
"email": "[email protected]",
"order_time": "2022-09-20 16:23:49",
"merchant_id": "MID629dcf88a4537",
"bank_id": null,
"gateway": "N/A",
"transaction_status": "Pending",
"card_scheme": null,
"attempts": 0,
"review_status": null
},
{
"id": 111,
"trans_id": "NP2209200435",
"order_id": "POWERRw8",
"full_name": "Customer/INSTANT",
"narration": "2035011W",
"amount": "10.0000",
"email": "[email protected]",
"order_time": "2022-09-20 16:23:49",
"merchant_id": "MID629dcf88a4537",
"bank_id": null,
"gateway": "N/A",
"transaction_status": "Pending",
"card_scheme": null,
"attempts": 0,
"review_status": null
},
{
"id": 111,
"trans_id": "NP2209200435",
"order_id": "POWERRw8",
"full_name": "Customer/INSTANT",
"narration": "2035011W",
"amount": "10.0000",
"email": "[email protected]",
"order_time": "2022-09-20 16:23:49",
"merchant_id": "MID629dcf88a4537",
"bank_id": null,
"gateway": "N/A",
"transaction_status": "Pending",
"card_scheme": null,
"attempts": 0,
"review_status": null
}
]
And I wanted to convert few of those data to array form like this:
[
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537'],
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537'],
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537'],
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537']
]
I've tried some sort solutions searched on SO, but still getting my normal tries, which are the following (Note: it added the second index into first index):
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537', 'NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537', 'NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537' ],
['NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537', 'NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537', 'NP2209200435', 'POWERRw8', 'Customer/INSTANT', '2035011W', '10.0000', 'MID629dcf88a4537' ]
I used:
let dataSet = [];
response.data.transactions.forEach((item) => {
dataSet.push(
item.trans_id,
item.order_id,
item.full_name,
item.amount,
item.transaction_status,
item.narration,
item.created_at
);
});
and
const trans_id = response.data.transactions.map(item => item.trans_id)
const order_id = response.data.transactions.map(item => item.order_id)
const full_name = response.data.transactions.map(item => item.full_name)
dataSet = [
trans_id,
order_id,
full_name
];
I need to populate it into datatable.