0

I have an array that looks like: [x, y]. While y will always remain constant. I want x to be incremented by 100 every iteration.

This is what I did:

let arr = [
    {
        "name": "pies",
        "data": [
            [
                "us",
                149045
            ],
            [
                "es",
                41746
            ],
            [
                "uk",
                37640
            ],
            [
                "au",
                16594
            ]
        ],
    },
    {
        "name": "cakes",
        "data": [
            [
                "us",
                128845
            ],
            [
                "es",
                35752
            ],
            [
                "uk",
                32246
            ],
            [
                "au",
                14333
            ]
        ],
    }
];

let inc = 100;
arr.forEach(d=> {
    d['m_arr'] = [inc, 125];
    inc +100;
});

console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

As you can see, it is not incrementing the value. It is always just inserting [100, 125]. In the first object with the name, pies I want the m_arr: [100, 125] and the next object with the name, cakes to be [200, 125].

How do I do that?

6
  • 1
    typo? inc +100 => inc += 100 Commented Apr 1, 2021 at 15:05
  • Why not use .map instead of .forEach? Commented Apr 1, 2021 at 15:05
  • @KooiInc that was it! Thanks for noticing! Commented Apr 1, 2021 at 15:06
  • I dont see any x or y here Commented Apr 1, 2021 at 15:07
  • @KaiLehmann, I used x and y as an example for array format Commented Apr 1, 2021 at 15:07

2 Answers 2

1

The problem is that your varible that stores the value the original 100 (inc) its new value is never set. This line inc + 100 will take the value of inc add 100 then forget it because its not stored. what you should be doing is using += to update the existing value.

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

Comments

1

I believe this is a simplified version of what you are trying to achieve here. Rather than setting up an inc variable which you may later need to reset if you plan to re-use the function, just use the iterator parameter included in the forEach method (2nd param). By adding 1 to this value and then multiplying by 100, you should always have the desired value. Here it is in action:

let arr = [
    {
        name: "pies",
        data: [
            ["us", 149045],
            ["es", 41746],
            ["uk", 37640],
            ["au", 16594]
        ],
    }, {
        name: "cakes",
        data: [
            ["us", 128845],
            ["es", 35752],
            ["uk", 32246],
            ["au", 14333]
        ],
    }
];

arr.forEach((d,i) => d.m_arr = [(i+1)*100, 125]);

console.log(arr); // returns the below ↓↓↓

[
    {
        name: "pies",
        data: [
            ["us", 149045],
            ["es", 41746],
            ["uk", 37640],
            ["au", 16594]
        ],
        m_arr: [100, 125]
    }, {
        name: "cakes",
        data: [
            ["us", 128845],
            ["es", 35752],
            ["uk", 32246],
            ["au", 14333]
        ],
        m_arr: [200, 125]
    }
];

Comments

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.