-1

I want to save what is returned from a function so when I call it again the functions return keeps saving but instead the saved return changes every time. Is there a way I could save the return of something so when I call it again the previous one is saved and the new one gets added. I wanted to use this when making an object using factory function which one parameter is made randomly, I want to save that random instance but the problem is if the file is called again that random instance is changed. I would be really grateful if anyone can show me a way I can save the output so when I run the js file again it gets added not replaced. For example:

let list = []

const pushingNum = (num) => {
  for (let x = 1; x <= num; x++) {
    list.push(x)
  }
}

pushingNum(2)

console.log(list)

The output I expect is [1,2,1,2,1,2] if I run the file three times but the output is [1,2] even after running the js file multiple times.

I also tried:

let list = []

const pushingNum = (array) => {
  for (const x of array) {
    list.push(x)
  }
}

pushingNum(['g', 'g', 'g'])

console.log(list)

Still the output is expected ['g', 'g', 'g', 'g', 'g', 'g'] after running the file two times but the output is ['g', 'g', 'g'].

Coder_IXJ
  • 11
  • 1
  • You create a new array every time. Why would expect that the array will persist but the rest of the code not? – Konrad Jan 22 '23 at 17:37
  • 1
    You'll need to provide more context for solutions, ie. where are you 'running the file', in the browser or locally via node? The crux of the matter is that you will need to persist the array state somewhere, either by writing to a file locally, or possibly using one of the [client side storage](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage) APIs provided by the modern DOM environment. – pilchard Jan 22 '23 at 17:38
  • I am running it locally via node – Coder_IXJ Jan 22 '23 at 17:41
  • In that case look at something like [How to update a value in a json file and save it through node.js](https://stackoverflow.com/questions/10685998/how-to-update-a-value-in-a-json-file-and-save-it-through-node-js) or [Save array value in a file](https://stackoverflow.com/questions/39322889/save-array-value-in-a-file) – pilchard Jan 22 '23 at 17:42
  • You probably want to save the data to the file or database. Because all the data in variables is removed when the process ends. Running a script multiple times, won't save the variables between runs – Konrad Jan 22 '23 at 17:43

2 Answers2

0

Store the previous state of the array in localStorage:

let list = JSON.parse(localStorage.getItem("array")) || []

const pushingNum = (num) => {
  for (let x = 1; x <= num; x++) {
    list.push(x)
  }
  localStorage.setItem("array", JSON.stringify(list))
}

pushingNum(2)

console.log(list)
Spectric
  • 30,714
  • 6
  • 20
  • 43
-1

you can solve it this way

let list = []
const pushingNum = (num) => {
  for (let x = 1; x <= num; x++) {
    list.push(1)
    list.push(2)
  }
}
pushingNum(2)
console.log(list)
starball
  • 20,030
  • 7
  • 43
  • 238