1

first post ever here

I'm trying to replicate this sort of JSON object so that I can store it inside localStorage with stringify:

I want to be able to create a new noteState inside of history whenever an event is caught but do not know necessarily how to go about doing so..

var history = {
    "noteState1": [
        {created: new Date(),
        modified: new Date(),
        left: "100",
        top: "200",
        text: "hello"},

        {created: new Date(),
        modified: new Date(),
        left: "150",
        top: "250",
        text: "new"},

        {created: new Date(),
        modified: new Date(),
        left: "200",
        top: "300",
        text: "world"}
    ], 
    "noteState2": [
        {created: new Date(),
        modified: new Date(),
        left: "100",
        top: "200",
        text: "hello"},

        {created: new Date(),
        modified: new Date(),
        left: "150",
        top: "250",
        text: "new"},

        {created: new Date(),
        modified: new Date(),
        left: "200",
        top: "300",
        text: "world"}
    ]
}

how can i create a new member with a single array inside history?

1
  • look at the JSON.parse and JSON.stringify methods. Commented Feb 14, 2012 at 21:53

3 Answers 3

5

You don't modify the json string - that's too risky. Instead you convert it back to a native javascript array/object, use the regular array push/pop type operators on it, then convert back to a json string.

Sign up to request clarification or add additional context in comments.

Comments

0

Why can't you just maintain your history as an array of objects and then add a new noteState? When you want to store you're array, simply insert each of the noteStates in the history array into a JSON object.

Comments

0

before you do anything, convert those json strings to objects. By doing that, you can easily just add namespaces to history like this:

history.noteStateX = { x:1,y:2 };

and now history has that property as well as the others.

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.