I have a function with params and I am doing a forEach loop to add all the values from the loop.
const data = (sd) => Object.entries(obj).map(([k, g]) => ({
['name']: k,
['data']: g.map(entry => entry[sd]),
['type']: sd
}));
I then push them into a giant array:
let arr = ['abc', 'xyz'];
let x = [];
arr.forEach(y => {
x = [...x, ...data(y)];
});
I also want to add another key-value pair to data. I want the value to come from an array:
color_array = ['red', 'blue', 'green']
So I did:
const data = (sd) => Object.entries(obj).map(([k, g]) => ({
['name']: k,
['data']: g.map(entry => entry[sd]),
['type']: sd,
['color']: color_arr[i++]
}));
Output:
color: "red"
data: (3) [1, 2, 3]
name: "Jim"
type: "Manager"
color: "blue"
data: (3) [1, 2, 3]
name: "Steve"
type: "Manager"
color: "green"
data: (3) [1, 2, 3]
name: "John"
type: "Manager"
--------------------NEW SET----------------
color: undefined
data: (3) [1, 2, 3]
name: "John"
type: "CEO"
color: undefined
data: (3) [1, 2, 3]
name: "John"
type: "COO"
When I do this, since I only have 5 elements in my array, the rest of the values go undefined. Is there any way for me to add elements from the array to start from the beginning of the color_arr?
I mean, for every x = [...x, ...data(y)];, Can the colors being added to data(y), always start from red, rather than just be valued as undefined?
nameinstead of a computed property name with a string of['name']? do you have an example of input an wanted output?