简体   繁体   中英

Add custom property to existing object

I have the following object which looks like this :

{ dateCreated: Fri Mar 18 2016 16:36:12 GMT+0400 (MUT),
 lastUpdated: Fri Mar 18 2016 16:36:12 GMT+0400 (MUT),
 users: [ '56ebdaad275a88df234a4480' ],
  viewOnlyUsers: [],
 __v: 0,
  owner: '56ebdaad275a88df234a4480',
 editUrl: 'e7f25e3d-be77-4cfa-a928-6af05f06ec44',
 viewUrl: '2669ba21-0e3b-4ac0-a886-74e3fe217af4?view',
 _id: '20c84dde-56b7-43ba-9fd3-510c1d24ba0f' }

How do i add a custom property to this object in Javascript?

I've tried

 workspaces[test] = 'test';

But it does not work.

Add quotes to the property name: workspaces['test'] = 'test';

However, this is fine too:

var test = 'myProp';
var workspaces = {}; // your object
workspaces[test] = 'test';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM