I am using .map()
to cycle through an object and places some values in to a 2nd object.
I am doing this as such:
function convertGraphItems(name, data) {
return ({ name, data, type: 'bar', stack: true });
}
var generatedArr = [...dataByAccountName.entries()].map(convertGraphItems);
However, I seem to be unable to assign 'name' and 'data' to their respective keys.
I have tried return ({ name: name, data: data, type: 'bar', stack: true });
but this does not achieve what I want.
My desired result should look like:
[
{
"name": "Savings",
"data": [
5474.18,
114031.26,
127890.72
],
"type": "bar",
"stack": true
}
]
But my current result is:
[
{
"name": [
"Savings",
[
5474.18,
114031.26,
127890.72
]
],
"data": 0,
"type": "bar",
"stack": true
}
]
Would anyone know how I could assign data
and name
to their respective keys?
EDIT: source array looks like:
[
{
"key": "Savings",
"value": [
5474.18,
114031.26,
127890.72
]
},
]
You are passing convertGraphItems
function into map
and you are expecting name
as key
and data
as value
, but this is not the case here.
In map
the first argument is the value and second is the index
. You have to make convertGraphItems
compatible so you can pass value externally as:
[...dataByAccountName.entries()].map(function ([_, { key, value }]) {
return convertGraphItems(key, value);
});
const dataByAccountName = [
{
key: "Savings",
value: [5474.18, 114031.26, 127890.72],
},
];
function convertGraphItems(name, data) {
return { name, data, type: "bar", stack: true };
}
var generatedArr = [...dataByAccountName.entries()].map(function ([, v]) {
const { key, value } = v;
return convertGraphItems(key, value);
});
console.log(generatedArr);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments