For loop overvalues on navigation from another page

Viewed 19

Good day, everyone, so when i refresh the page forcibly (ctrl + r) i'm taking exact result what i need, BUT the problem is when i'm going to another app's page and back ELSE statement fith subArray2 is going crazy and i dont undestant why (subArray1 is works good).. At the bottom i've pin logs

The idia is if some of the item length is more then 1 ([ "100", "-150", "11", "111" ], [ "Debt", "Salary", "Credit", "sdad" ]) push them to another array with a same format and later combine together.

When firts call of console.log(monthlyValues[i].length) its length are [1, 4, 1, 1] at next call(going thougth pages) length is [2, 8, 2, 2] and increasing with each call

data:

let subArray1 = [];
let subArray2 = [];

let monthlyValues = [
    [ "-50" ],
    [ "100", "-150", "11", "111" ],
    [ "-200" ],
    [ "290" ]
];

let categories = [
    [ "Salary" ],
    [ "Debt", "Salary", "Credit", "sdad" ],
    [ "Salary" ],
    [ "Investments" ]
];

Computation:

for (let i = 0; i < monthlyValues.length; i++) {
  if(monthlyValues[i].length == 1) {
    subArray1.push([
      {
        "name": monthlyCategories[i],
        "value": +monthlyValues[i] 
      }
    ])
  } else {
    for (let j = 0; j < monthlyValues[i].length; j++) {
      subArray2.push({
          "name": monthlyCategories[i][j], 
          "value": +monthlyValues[i][j] 
        });
      };
    };
  subArray1.push(subArray2);
};

Log of the first call: (this is what i need)

[
    {
        "name": "Debt",
        "value": 100
    },
    {
        "name": "Salary",
        "value": -150
    },
    {
        "name": "Credit",
        "value": 11
    },
    {
        "name": "sdad",
        "value": 111
    }
]

Secont log call: (this is bad one, and each time i call the page length is increaseing)

[
    {
        "name": "Salary",
        "value": -50
    },
    {
        "name": "Salary",
        "value": -50
    },
    {
        "name": "Debt",
        "value": 100
    },
    {
        "name": "Salary",
        "value": -150
    },
    {
        "name": "Credit",
        "value": 11
    },
    {
        "name": "sdad",
        "value": 111
    },
    {
        "name": "Debt",
        "value": 100
    },
    {
        "name": "Salary",
        "value": -150
    },
    {
        "name": "Credit",
        "value": 11
    },
    {
        "name": "sdad",
        "value": 111
    },
    {
        "name": "Salary",
        "value": -200
    },
    {
        "name": "Salary",
        "value": -200
    },
    {
        "name": "Investments",
        "value": 290
    },
    {
        "name": "Investments",
        "value": 290
    }
]
0 Answers
Related