0

Hello i am quite new to javascipt so please explain things clearly. I am currently running a php page which includes:

<input type="text" id="data"/>

<script>
document.getElementById("data").value = localStorage.getItem('itemsArray');
</script>

this items array contains objects which is saved like this:

function save(){

var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];

var newItem = {};
var num = document.getElementById("num").value;

newItem[num] = {
    "methv": document.getElementById("methv").value
    ,'q1': document.getElementById("q1").value,
    'q2':document.ge27548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]tElementById("q2").value,
    'q3':document.getElementById("q3").value,
    'q4':document.getElementById("q4").value,
    'comm':document.getElementById("comm").value
};


oldItems.push(newItem);

localStorage.setItem('itemsArray', JSON.stringify(oldItems));}

the result of the page appears like this:

[{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]

is there anyway i can split the data so I can manipulate it one at a time like a loop or something. For example:

1st time:

{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}

Next:

{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}

etc.

Thanks.

jgillich
  • 71,459
  • 6
  • 57
  • 85
user2162768
  • 69
  • 1
  • 7
  • You can split the string on any char or substring: http://stackoverflow.com/questions/96428/how-do-i-split-this-string-with-javascript – isherwood Mar 15 '13 at 14:13

3 Answers3

2

You should JSON.parse() it like the save() method does when filling the oldItems array, then you can cycle the resulting array.

Example code:

<input type="text" id="data"/>

<script>
 var myArray = JSON.parse(localStorage.getItem('itemsArray')) || [];
 for (var i = 0; i < myArray.length; i++) {
    var element = myArray[i];
    // Do something with element.
 }
</script>
DocKuro
  • 445
  • 4
  • 13
  • hey man ive just tried this and tried alerting it our and i get '[Object object]' any ideas? – user2162768 Mar 15 '13 at 14:29
  • That's just the standard output notation for javascript objects. If you want to see what's inside you have to use a console, like firebug or the developer tools in chrome. – DocKuro Mar 15 '13 at 14:38
1

The data is already returned in an array, which you can loop through with a standard for loop. However, you'll want to parse it first so that you then have an object that you can access using standard object methods.

For example:

var allItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
for(var i = 0; i < allItems.length; i++) {
  var item = allItems[i];
  console.log('Current item: %o', item);
  // do whatever you want to it, etc.
}
0

Hi It looks like your save script is getting data from textfields and adding them as objects within an array.

the array is stored in your local storage and you can get it like this:

var items = JSON.parse(localStorage.getItem('itemsArray')) || [];

As this is an array you should be able to loop through it with a simple for loop:

for(var i in items){
  // this has the i object within the array
  var item = items[i];

  // if you dont know the names of the keys in the array
  // you can loop through this again using another loop
  for(var j in item){
    // you can then change this key like so:
    items[i][j] = item[j].toUpperCase(); // (this makes the value upper-case for example)
  }

  // if you do know the names then you can just change them directly
  items[i].q1 = items[i].q1.toUpperCase();

}
Thomas Smart
  • 386
  • 3
  • 10