0

Hello i am currently running a function in my javascript called 'save' which has the functionality of:

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.getElementById("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));

and currently the format of this comes out like this:

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

is there any way i can change this to a format to something like:

{1173627548,dont know, -,-,U,-,} Thanks

4

1 回答 1

1

您所要做的就是使用数组而不是对象:

newItem = [
    num,
    document.getElementById('methv').value,
    document.getElementById('q1').value,
    document.getElementById('q2').value,
    document.getElementById('q3').value,
    document.getElementById('q4').value,
    document.getElementById('comm').value
];
于 2013-03-16T16:23:00.733 回答