문제

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));    
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top