Answers for "dynamic object property name javascript"

2

dynamic properties instead javascript

var foo = { pName1 : 1, pName2 : [1, {foo : bar }, 3] , ...}

var name = "pName"
var num  = 1;

foo[name + num]; // 1

// -- 

var a = 2;
var b = 1;
var c = "foo";

foo[name + a][b][c]; // bar
Posted by: Guest on June-23-2020
0

dynamic object property name javascript

let me = {
  name: 'samantha',
};

// 1. Dot notation
me.name; // samantha

// 2. Bracket notation (string key)
me['name']; // samantha

// 3. Bracket notation (variable key)
let key = 'name';
me[key]; // samantha
Posted by: Guest on October-11-2020

Code answers related to "dynamic object property name javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language