How do I assign values to keys when I merge 2 objects with .map()?

MeltingDog

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
        ]
    },
]
decpk

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I merge two objects such that keys from one map to values of another, without the extra keys in the second, using latest Javascript?

How do i assign and obtain values to objects created in an array?

how do I replace values of specific keys in an array of objects? javascript

How do I map keys and values of a dictionary to json array?

How do I assign named keys to arrays?

How do I iterate though an array of objects with keys and values in react and render them in the JSX( Both keys and Values)

How do I assign the same array values to 2 different variables

How Do I Assign Values of 2 or more Fields in VBScript

How can i merge objects with their values in MongoDB

How do I map from a Typescript Enum to objects I generate based off Enum keys and properties?

How do I exclude null values when merging objects by key?

How do I merge an array of objects in Javascript?

How do I assign values to cells?

How can I merge a sequence of objects with repeating keys into one?

How do I enumerate the keys and values of a Hashtable?

How do I exchange keys with values in a dictionary?

How do i print keys without Values

How do I assign 2 different variable to the return values from a function that return 2 values?

How do i get values of id’s And also how do i merge multiple objects into single array into javascript

I want to declare the values of the objects within the class and only call the keys when Im declaring a new class. What could I do?

How do I return array of keys that have only empty values in the objects?

How do I map tables to Java objects

How do I map an array of objects

Merge 2 maps with keys as values of second map

How do I merge two objects while omitting null values with lodash

How do I merge the the two values of two objects that share a common key together and return it?

How do I efficiently map keys from one dataset based on values from other dataset

Java How do I get all map values within specific keys?

How do I rename a key in a Map Object, while keeping the value and the other keys and values the same?

TOP Ranking

HotTag

Archive