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

2
  • Of course you can write it like this but this wouldn't be JSON anymore. How would you parse it ? Commented Mar 16, 2013 at 16:03
  • So you want to strip the attribute names, structure and quotes? As dystroy mentioned, that's not JSON in the slightest, and you'll have trouble getting data back out of it if your format changes. Also you won't be able to write anything that has a comma in it without escaping somehow. If you want to do this, you might as well make your own function to write the string. Commented Mar 16, 2013 at 16:06

1 Answer 1

1

All you have to do is use an array instead of an object:

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
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.