0

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":"[email protected]",
    "body":"hello",
    "active":true,
    "confidential":false,
    "numberOfDaysToWait":1,
    "sequentialOrder":3
    },
    {
    "EmailType":"Create New",
    "name":"sweeeee",
    "subject":"eeee",
    "from":"[email protected]",
    "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);

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

and what about the template object inside it ?
@swapniljain Updated
0

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": "[email protected]",
      "body": "hello",
      "active": true,
      "confidential": false,
      "numberOfDaysToWait": 1,
      "sequentialOrder": 3
    },
    {
      "EmailType": "Create New",
      "name": "sweeeee",
      "subject": "eeee",
      "from": "[email protected]",
      "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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.