1

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?

2
  • why not take name instead of a computed property name with a string of ['name']? do you have an example of input an wanted output? Commented May 10, 2020 at 19:56
  • @NinaScholz, I am not sure what you mean Commented May 10, 2020 at 19:57

1 Answer 1

1

You can loop over the values in the color array by using the modulo. That is, the remainder of dividing your counter by the color array's length.

const color_array = ['red', 'blue', 'green', 'black', 'white'];
let i = 0;
for (j = 0; j < 20; j++) {
   console.log(color_array[i++ % color_array.length]);
}

Untested version of your code:

const data = (sd) => Object.entries(obj).map(([k, g]) => ({
        ['name']: k,
        ['data']: g.map(entry => entry[sd]),
        ['type']: sd,
        ['color']: color_array[i++ % color_array.length]
}));

More information on the % operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way to restart the array with every x = [...x, ...data(y)];. So every time data(y) is pushed, Can the values from color array being added start from the beginning?
You can reset i = 0 inside your loop.
Sorry, how do I do that?
You can define the variable i above your loop instead and set it to zero for each y. For example: let i; arr.forEach(y => { i = 0; x = [...x, ...data(y)]; });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.