-1
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)

I need fileName property to be dynamic What is need is, like below

{
 "test.c":"Case1"
}

Can any one help me

Nijesh W
  • 39
  • 1
  • 6

2 Answers2

1
test.fileName = testCase;

Won't work in this case. Should be

test[fileName] = testCase;
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You can use the ES6 computed property syntax:

{
    [fileName]: "Case1"
}

This will be interpreted dynamically as:

{
    "test.c": "Case1"
}
trincot
  • 317,000
  • 35
  • 244
  • 286