I'm Having a JSON that is getting creates in for-loop
.
Here my main requirement is to create the Key
from a predefined variable.
Here is the code that I'm using.
var rows =["Hello Trt", "Ho there", "I'm up"];
var name="Hello";
var jsonData = {};
var intentName=[];
var mainPersonel = {};
var intents = {};
intents.intentName = rows;
mainPersonel.intents=intents;
console.log(JSON.stringify(mainPersonel));
Looking into the other SO posts, I'm able to find on how can I replace intents
variable, but here in my case, I want to replace intentName
with name
in the output.
Expected output is
{"intents":{"Hello":["Hello Trt","Ho there","I'm up"]}}
Please let me know on how can I achieve this.
Thanks
I think the below code satisfies your requirement.
var rows =["Hello Trt", "Ho there", "I'm up"];
var name="Hello";
var jsonData = {};
var intentName=[];
var mainPersonel = {};
var intents = {};
intents[name]= rows;
mainPersonel.intents=intents;
console.log(JSON.stringify(mainPersonel));
You can do like this:
intents[name]= rows;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With