I am trying to make a chart that gets data from an API endpoint with open, close, high, and low data for a bar chart like the ones you see for stocks. I make the endpoint request, and use .map to reach for example the value for low.
The issue is that the result needs to be multiplied by 213 before being displayed on the chart, how can I write a code that takes the value of every single one of these arrays, multiplies the inner value by 213, and puts it back in the original form so the chart can use it?
code used to reach the data:
const usdlow =response2.data.data.ethereum.Trades.map(el => ({low: el.low}))
*example
console.log(usdopen[1])
result:
{open: 6.606731585128299}
There are about 5000 arrays for different points.
I basically want to get this open value out, multiply it by 213, and then put it back in so the chart can take it and use it.
I have not been able to get this to go back to the original form so chart can use it, I believe the method I use doesn't work for arrays like this. I would appreciate any help, thanks for your time.
Actually, it's easy. If you have an array of objects, you can just use the spread operator to spread the rest of the object and multiply the values you'd like to change by 213:
let arr = [
{ key: 1, value: 10 },
{ key: 2, value: 103 },
{ key: 3, value: 45 },
{ key: 4, value: 89 },
];
arr = arr.map((item) => {
return { ...item, value: item.value * 213 };
});
console.log(arr);
// expected output: [ { key: 1, value: 2130 }, { key: 2, value: 21939 }, { key: 3, value: 9585 }, { key: 4, value: 18957 } ]
Et voilà... values changed. 🤓