Assign variable content to object property name

user6064424

I want to create a json object in node.js that looks like this;

{ WantThisName: { property_length: 8 } }

Here is my code;

var property_name = "WantThisName"
var length = 8;

var Obj_bin;
Obj_bin= {property_name: {property_length:length} };
console.log (Obj_bin);

The console output is;

{ property_name: { property_length: 8 } }

The problem lies with property_name not getting the contents of the variable property_name.

thebjorn

You can use computed (dynamic) property names:

> foo = "foozz"
'foozz'
> {[foo]: 42}
{ foozz: 42 }

They're part of the Enhanced Object Literals of es2015 (https://github.com/lukehoban/es6features#enhanced-object-literals).

For your example:

> var property_name = "WantThisName";
var property_name = "WantThisName";
undefined
> var length = 8;
undefined
> {[property_name]: { property_length: length}}
{ WantThisName: { property_length: 8 } }
>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically assign property name to object

From Dev

Javascript object property with variable name

From Dev

Javascript object property with variable name

From Dev

Assign a variable to a property and modify the real object in QML

From Dev

Using variable *name* as property name in object literal

From Dev

Push an object with a variable as a property name onto an array

From Dev

Access static object property through variable name

From Dev

Vue 3 access object property by variable name

From Dev

Selecting an object property by a name stored in a variable

From Dev

Vue 3 access object property by variable name

From Dev

Using a variable for an object property name in an array?

From Dev

variable object property name as parameter in javascript?

From Dev

Using a variable as the property name for another object in handlebars

From Dev

How to assign a context-variable to a property with the same name in QML?

From Java

How to check if object property exists with a variable holding the property name?

From Dev

How to get an object property value when property name is in a variable?

From Dev

python, how to assign object of a class already created with name in string variable

From Dev

Javascript - Assign (not clone) a large object to a new variable to reduce its name

From Dev

php use object property value as variable variable name

From Dev

How can I assign a variable's content to a variable's name in perl?

From Dev

Object assign with dynamic variable

From Dev

Serialize c# object in XML using variable content as attribute name

From Dev

How do I use a variable for a property name in an object initializer/literal?

From Dev

'undefined' variable works as key to object with 'undefined' property name

From Dev

How to add an object property inside an array using a variable name for javascript?

From Dev

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

From Dev

JQuery assign HTML content to variable

From Dev

JQuery assign HTML content to variable

From Dev

Syntax to assign a method to a variable/property

Related Related

  1. 1

    Dynamically assign property name to object

  2. 2

    Javascript object property with variable name

  3. 3

    Javascript object property with variable name

  4. 4

    Assign a variable to a property and modify the real object in QML

  5. 5

    Using variable *name* as property name in object literal

  6. 6

    Push an object with a variable as a property name onto an array

  7. 7

    Access static object property through variable name

  8. 8

    Vue 3 access object property by variable name

  9. 9

    Selecting an object property by a name stored in a variable

  10. 10

    Vue 3 access object property by variable name

  11. 11

    Using a variable for an object property name in an array?

  12. 12

    variable object property name as parameter in javascript?

  13. 13

    Using a variable as the property name for another object in handlebars

  14. 14

    How to assign a context-variable to a property with the same name in QML?

  15. 15

    How to check if object property exists with a variable holding the property name?

  16. 16

    How to get an object property value when property name is in a variable?

  17. 17

    python, how to assign object of a class already created with name in string variable

  18. 18

    Javascript - Assign (not clone) a large object to a new variable to reduce its name

  19. 19

    php use object property value as variable variable name

  20. 20

    How can I assign a variable's content to a variable's name in perl?

  21. 21

    Object assign with dynamic variable

  22. 22

    Serialize c# object in XML using variable content as attribute name

  23. 23

    How do I use a variable for a property name in an object initializer/literal?

  24. 24

    'undefined' variable works as key to object with 'undefined' property name

  25. 25

    How to add an object property inside an array using a variable name for javascript?

  26. 26

    Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

  27. 27

    JQuery assign HTML content to variable

  28. 28

    JQuery assign HTML content to variable

  29. 29

    Syntax to assign a method to a variable/property

HotTag

Archive