0

I'm having problem with my data property.

data: function() {
        return {
            conversations: 
            [

            ]
        }
}

When I make a request to server i get response with data. I store these items in response.data.conversations. It has property named response.data.conversations[].messages which is saved in JSON notation and stores multiple items. I don't know how to ADD new items to that field.

That's what I have tried:

for(var i=0; i<response.data.conversations.length; i++) {
    for(var j=0; j<this.conversations.length; j++) {
        if(response.data.conversations[i].conversation_id == this.conversations[j].conversation_id) {
            var obj = JSON.parse(this.conversations[j].messages);
            obj.push(JSON.parse(response.data.conversations[i].messages));
            this.conversations[j].messages = JSON.stringify(obj); 
        }
    }
 }
12
  • How to add new items to the messages field? It all depends on what kind of data is in the messages field. Commented Feb 11, 2016 at 21:23
  • There is another JSON. Commented Feb 11, 2016 at 21:28
  • JSON is just a string. Do you mean another object? Because if so, we need to see what that object looks like. Commented Feb 11, 2016 at 21:36
  • A single message: { "author": "test2", "content": "Test Message 3", "createdAt": { "date": "2016-02-02 00:00:00", "timezone_type": 3, "timezone": "Europe/Paris" } } Commented Feb 11, 2016 at 21:46
  • So messages is only one message? Then you can't really add a new object to that. Instead, you want an array of messages. If you can an array, you can do .messages.push(newMessage) Commented Feb 11, 2016 at 21:48

0

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.