0

您好,我对 javascipt 很陌生,所以请解释清楚。我目前正在运行一个 php 页面,其中包括:

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

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

这个项目数组包含这样保存的对象:

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));}

页面的结果如下所示:

[{"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":""}}]

无论如何我可以拆分数据,这样我就可以像循环或其他东西一样一次操作它。例如:

第一次:

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

下一个:

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

等等

谢谢。

4

3 回答 3

2

您应该 JSON.parse() 它就像填充 oldItems 数组时的 save() 方法一样,然后您可以循环生成的数组。

示例代码:

<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>
于 2013-03-15T14:19:34.470 回答
1

数据已经在一个数组中返回,您可以使用标准的 for 循环遍历该数组。但是,您需要先对其进行解析,然后才能拥有可以使用标准对象方法访问的对象。

例如:

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.
}
于 2013-03-15T14:27:00.980 回答
0

您好 看起来您的保存脚本正在从文本字段中获取数据并将它们作为对象添加到数组中。

该数组存储在您的本地存储中,您可以像这样获取它:

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

由于这是一个数组,您应该能够通过一个简单的 for 循环来遍历它:

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();

}
于 2013-03-15T14:22:00.380 回答