I have the data like the below from API.I want to format the data. can anyone will help?
fields: [
{
name: "A",
values: {
data: [1, 2, 3, 4, 5]
}
},
{
name: "B",
values: {
data: [6, 7, 8, 9, 10]
}
}
]
how to get the output like this
var d = [[1,6],[2,7],[3,8],[4,9],[5,10]]
2
Answers
If you have the same array index and everything is static and not dynamic except the values, you can do something like this:
Assumptions:
obj
as the object that stores the response.fields[X].values.data
.fields[0].values.data
andfields[1].values.data
.Output
If the number of fields is dynamic instead of just two: you can iterate over the length of the first field’s array in an outer loop, then iterate over each field in an inner loop to pick out the number within that field’s array at the index from the outer loop (after first verifying that the length of each number array is the same for every field):
Code in TS Playground