Thursday 20 April 2017

How to set value to the property that does not exist by creating new property in Javascript object?

I have Javascript object as follows.

var data = {
      USER_LIKES: 
       { userId1: 
          { publishedContentId: 1492688420796,
            publishedContentId1: 'TIMESTAMP',
            publishedContentId2: 1492688750874 },
         userId2: 
          { publishedContentId: 1492688611936,
            publishedContentId1: 1492688730807,
            publishedContentId2: 'TIMESTAMP' } 
       }
    } 

I can add more contents under existing userIds as follows.

data["USER_LIKES"]["userId1"]["publishedContentId3"] = admin.database.ServerValue.TIMESTAMP;

But I want to add contents under userId that does not exist in the object. For example I want to add content under userId3(not exist).

I have tried to do the same thing as follows

data["USER_LIKES"]["userId3"]["publishedContentId3"] = admin.database.ServerValue.TIMESTAMP;

but returns error TypeError: Cannot set property 'publishedContentId1' of undefined.

So how can I achieve this?



via isuru

No comments:

Post a Comment