this is my data which i am getting through the form.
var subject = {
"items":[
{
"EmailType":"INVITATION",
"name":"INVITATION",
"subject":"Welcome to Transcendental Meditation India",
"from":"noreply.globalwebsite@tm.org",
"body":"hello",
"active":true,
"confidential":false,
"numberOfDaysToWait":1,
"sequentialOrder":3
},
{
"EmailType":"Create New",
"name":"sweeeee",
"subject":"eeee",
"from":"swa@mail.com",
"body":"hello2",
"active":false,
"confidential":true,
"numberOfDaysToWait":1,
"sequentialOrder":2
}
]}
I am using the loop to create another array of object which looks like this after modifying subject.
"Catitems": [
{
"template": {
"name": "Series 1 email",
"from": "TEAMGMG",
"subject": "GROUP2 - SERIES1 - EMAIL",
"body": "<html><body><strong>My test email</strong></body></html>",
"confidential": true,
"active": true
},
"sequentialOrder": 1,
"numberOfDaysToWait": 0,
}, {
"template": {
"name": "Series 2 email",
"from": "TEAMGMG",
"subject": "GROUP2 - SERIES2 - EMAIL",
"body": "<html><body><strong>My test email2</strong></body></html>",
"confidential": true,
"active": true
},
"sequentialOrder": 2,
"numberOfDaysToWait": 10,
}
]
I Have tried to manipulate the Subject with this loop but can not be able to set the property.
var Catitems={};
for(var i=0; i<subject.items.length ; i++){
Catitems[i]["name"]= subject.items[i].EmailType
}
console.log(item);
Your Catitems
is declared as object , when it should be declared as an array :
var Catitems=[];
for(var i=0; i<sobject.items.length ; i++){
var tempObj = {
"template":{} //must set this otherwise some other error
};
tempObj["template"]["name"] = sobject.items[i].EmailType
//tempObj["template"]["somethingElse"] = sobject.items[i].somethingElse
Catitems.push(tempObj);
}
//console.log(item); //not defined btw
console.log(Catitems);
If you want to modify each one of the element in the array, you can use Array.map to map each item in the array to a new obj structure.
var subject = { "items": [{ "EmailType": "INVITATION", "name": "INVITATION", "subject": "Welcome to Transcendental Meditation India", "from": "noreply.globalwebsite@tm.org", "body": "hello", "active": true, "confidential": false, "numberOfDaysToWait": 1, "sequentialOrder": 3 }, { "EmailType": "Create New", "name": "sweeeee", "subject": "eeee", "from": "swa@mail.com", "body": "hello2", "active": false, "confidential": true, "numberOfDaysToWait": 1, "sequentialOrder": 2 } ] } const CartItems = subject.items.map((item) => { return { name: item.EmailType, template: { name: item.EmailType, from: item.from, subject: item.subject, body: item.body, confidential: item.confidential, active: item.active }, sequentialOrder: item.sequentialOrder, numberOfDaysToWait: item.numberOfDaysToWait, }; }); console.log(CartItems)
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.