Question

I have this Object containing pretty much anything i want to know about a workout. Yet when I Stringify the object the array-objects aren't stored or something.

This is my object i want to stringify:

Object {name: "testWorkout", program: Array[4]}
name: "testWorkout"
program: Array[4]
        first: Array[3]
        slope: 0
        speed: "medium"
        time: 10
        length: 3
        __proto__: Array[0]

        interval1: Array[4]
        radio: "Time"
        size: "33"
        slope: "79"
        speed: "fast"
        length: 4
        __proto__: Array[0]

        interval2: Array[4]
        radio: "Time"
        size: "3322"
        slope: "16"
        speed: "medium"
        length: 4
        __proto__: Array[0]

        last: Array[3]
        slope: 0
        speed: "medium"
        time: 20
        length: 3

    __proto__: Array[0]
    length: 4
    __proto__: Array[0]
__proto__: Object

But instead it stringifies nice it does stringify as:

{"name":"testesr","program":[null,null,null,null]} 

How to solve this with pure JavaScript? So without JQuery?

These are my stringify and read method:

function putInLocalStorage(){
    console.log(JSON.stringify(workout));
    localStorage.setItem('workout-'+workout.name, JSON.stringify(workout));
}

function readLocalStorage(){
    var retrievedObject = localStorage.getItem('workout-'+workout.name);
    console.log('retrievedObject: ', JSON.parse(retrievedObject));    
}
Était-ce utile?

La solution

change your object looking as below then stringify. It should work.

Object {name: "testWorkout", program: {}}

Point here is normal JavaScript arrays are designed to hold data with numeric indexes. You can stuff named keys on to them but the JSON array data type cannot have named keys on an array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top