0

I have two javascript variables. One contains a content object and the other an array of content objects. If the contents object already has contents then I can do a push and add to it. I have this working okay in the first part of my if statement.

However if the contents object is null then the push does not work. How can I make it so that I can add just one content to the null contents object. In other words how can I create a contents object and assign self.content to it in the same time. I have tried here but it is not working:

            if (self.contents) {
                self.contents.push(self.content);
            } else {
                self.contents = [self.content]
            }
2
  • 11
    It's not javascript. Commented Aug 10, 2015 at 7:46
  • 1
    Check my answer, if its not working for you you've misspelled something, or are not describing the case properly. Commented Aug 10, 2015 at 8:17

1 Answer 1

0
if (self.contents) {
    self.contents.push(self.content);
} else {
    self.contents = [self.content];
}
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.