0

enter image description here

I have this JSON array. I am displaying data in a table. So, How should I put 'transactionData' inside the main object? i.e. I want the object as

{
completeTime: "value",
createTime: "value2",
assigneeName:"value3",
assigneeMap:"value4"
}

Since this is a JSON Array, so I need a way to iterate the array and make each of the objects as required.

I can't use the original JSON array since the object transactionData is not fixed and its keys might change. So, I don't want to hardcode any value like assigneeMap or assigneeName as it might change. Hence, I want whatever the values in transactionData object are there, I want to insert it into my main object.

BeaST 30
  • 696
  • 1
  • 10
  • 22

2 Answers2

1

use the array.map(), something like this

results.map(x=>{
{
completeTime: x.completeTime,
createTime: x.createTime,
assigneeName: x.assigneeName || x.assigneename,
assigneeMap:x.assigneeMap || x.assigneeMap || x.yourChangedKey,
}
})
Kakul Sarma
  • 303
  • 1
  • 4
  • 21
0

Use Array.prototype.map()

const result = arr.map(item => {
    const { completeTime, createTime, data: { transactionData } } = item;
    const { assigneeName, assigneeMap } = transactionData;
    return {
        completeTime,
        createTime,
        assigneeName,
        assigneeMap
    }
});
Karan Singh
  • 400
  • 1
  • 2
  • 13