admin管理员组

文章数量:1427458

This may be a really quick answer (or a really stupid question), but I'm trying to store an array of integers so that they can be accessed later locally by another function, at a random time.

Ways I have thought of so far to do this (and their flaws):

  • HTML5 data attributes i.e. data-ids="1,2,3" (can't store an array easily in these)
  • HTML5 localStorage (can only store a string, not an array and would have to convert)
  • a hidden input i.e. <input type="hidden"> (again, can't store an array, have to convert into a string)

Ideally I would like to be able to push values onto this locally stored array with syntax like array.push(value) etc.

Is there an easy way to do this that I'm missing or will I be resorting to hacks? The end use of this array will be paring with another array of integers to see if any values match, and if there is a match, remove the index from the second array (i.e. it's an array filter).

This array shouldn't be stored on the server because it is different for each user on the client-side. If there is no nice way to do this I'll probably just think about implementing the functionality a different way.

This may be a really quick answer (or a really stupid question), but I'm trying to store an array of integers so that they can be accessed later locally by another function, at a random time.

Ways I have thought of so far to do this (and their flaws):

  • HTML5 data attributes i.e. data-ids="1,2,3" (can't store an array easily in these)
  • HTML5 localStorage (can only store a string, not an array and would have to convert)
  • a hidden input i.e. <input type="hidden"> (again, can't store an array, have to convert into a string)

Ideally I would like to be able to push values onto this locally stored array with syntax like array.push(value) etc.

Is there an easy way to do this that I'm missing or will I be resorting to hacks? The end use of this array will be paring with another array of integers to see if any values match, and if there is a match, remove the index from the second array (i.e. it's an array filter).

This array shouldn't be stored on the server because it is different for each user on the client-side. If there is no nice way to do this I'll probably just think about implementing the functionality a different way.

Share Improve this question asked Jun 8, 2013 at 20:22 wnajarwnajar 7571 gold badge7 silver badges27 bronze badges 5
  • 1 Lastly... please be honest and tell me if this is a really dumb way to do things :) I won't be offended, it will just save me a lot of time. – wnajar Commented Jun 8, 2013 at 20:24
  • That's a matter of scope. Where are you declaring the array? – bfavaretto Commented Jun 8, 2013 at 20:25
  • 2 create a javascript array (in public scope) and store the array in that? – Johan Commented Jun 8, 2013 at 20:25
  • I'm declaring the array in a function that submits a post via AJAX, and I need to be able to access the array in a separate polling function that recurs every n seconds and retrieves data from the server. I.e. we pare the two arrays on every AJAX poll. – wnajar Commented Jun 8, 2013 at 20:26
  • use a singleton/global variable? – cmt Commented Jun 8, 2013 at 20:27
Add a ment  | 

4 Answers 4

Reset to default 6

You could, of course, just use window.somevarname = [1,2,3]

Alternatively, if by "elsewhere" you mean on a pletely different pageload, then your best bet would be to run it through JSON.stringify() and drop it in localStorage, then JSON.parse() it out.

Just declare a global variable in your html head section

<script type="text/javascript">
var MyGlobalVariable = [1,2,3];
// or window.MyGlobalVariable = [1,2,3];
</script>

Now you can access it throughout the page.

create a javascript variable in the global scope(or whereever the array is needed) and you can access it by it's name.

It's totally fine to store data in global java script variables, but the deciding factor is how you navigate between pages. Javascript is typically not a good choice to store state if you're planning on doing full page refreshes. However it works well for Ajax UIs where global variables are preserved when sub sections on the larger page are loaded

本文标签: Javascript store an array of integers locally so they can be accessed elsewhereStack Overflow