0

I am facing an issue in small requirement. I want to create a JSON array in below format using JavaScript for loops.

var payloadTest = {
        "people": [{
                "pic": "sap-icon://employee",
                "name": "Ravi",
                "role": "team member",
                "appointments": [{
                        "start": new Date("2017", "0", "21", "0", "0"),
                        "end": new Date("2017", "0", "21", "23", "59"),
                        "title": "Meet John Miller"
                    }, {
                        start: new Date("2017", "0", "18", "0", "0"),
                        end: new Date("2017", "0", "18", "23", "59"),
                        title: "Team meeting",
                    }]
            ]
        }
    };

Im trying with below code using for loops. But it is not working.

var itemsArr = [];
var headArr = [];
for (var i = 0; i < resultSet.length; i++) {
    var obj = {};
    var itm={};
    itm.pic="sap-icon://employee";
    itm.name=resultSet[i].Rowlabel;
    itm.role=resultSet[i].RowId;
    headArr.push(itm);

    obj.start=resultSet[i].Begda;
    obj.end=resultSet[i].Endda;
    obj.title=resultSet[i].RowId;
    obj.type="Type02";
    obj.tentative=false;
    itemsArr.push(obj);
    //headArr.push(itemsArr);
}
payloadTest.people = headArr;
payloadTest.people.appointments = itemsArr;

Can someone please help me to create an array using for loops in the above JSON Format.

Note :: appointments array count may increase based on the results coming from backend

1
  • Where do you get multiple appointments for each person from? You're getting the appointment from the same element of resultSet as the person. Commented May 5, 2018 at 7:16

2 Answers 2

2

In:

payloadTest.people.appointments = itemsArr;

you are trying to assign the appointment array as a property of an array of people. Instead, you should be assigning it as a property of an (or every?) individual person.

So you could do:

payloadTest.people[0].appointments = itemsArr;

but that would only give one of the people an array of appointments. It's unclear from your code whether you want each person to have a reference to (or copy of) the same appointment array, one person to have it, or whether you thought you were creating separate arrays for each person. To do the later, you'd need a nested loop.

Update

You probably want to move the declaration/initialization of itemsArr inside the loop, so that each person has their own array (otherwise, modifications to one person's appointment array would be made to everyone's array).

Then, you would likely want to initialize them with a unique test array - so a nested loop (say j from 0 to i), creating incrementally larger lists of appointments for each person. But as it's just test data you're creating, it's really up to you what belongs in there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you dough for your quick response. It is working
1

Change your code according to below code

for (var i = 0; i < resultSet.length; i++) {
        var obj = {};
        var itm={};
        itm.pic="sap-icon://employee";
        itm.name=resultSet[i].Rowlabel;
        itm.role=resultSet[i].RowId;
        headArr.push(itm);

        obj.start=resultSet[i].Begda;
        obj.end=resultSet[i].Endda;
        obj.title=resultSet[i].RowId;
        obj.type="Type02";
        obj.tentative=false;
        itemsArr.push(obj);
        itm.appointments = itemsArr;  // Added
        //headArr.push(itemsArr);
    }
    payloadTest.people = headArr;
    /*payloadTest.people.appointments = itemsArr;*/  // Removed

Removed

payloadTest.people.appointments = itemsArr;

Because it assigns appointments to person array not to each person. You have to add appointments to each person record to added in loop

itm.appointments = itemsArr;

See below snippet for your multiple appointment. I used dummy data. Change according to your data format.

var headArr = [];
  var payloadTest = [];

  var resultSet = [{'Rowlabel' : 'Ravi', 'RowId' : 'team member', 'appointments' :[{'Begda' : 'test', 'Endda' : '1'}, {'Begda' : 'test', 'Endda' : '1'}]}, {'Rowlabel' : 'name', 'RowId' : '1', 'appointments' : [{'Begda' : 'test', 'Endda' : '1'}]}];
  
  for (var i = 0; i < resultSet.length; i++) {
      var itm={};
      itm.pic="sap-icon://employee";
      itm.name=resultSet[i].Rowlabel;
      itm.role=resultSet[i].RowId;

      var itemsArr = [];
      for (var j = 0; j < resultSet[i].appointments.length; j++) {
          var obj = {};
          obj.start=resultSet[i].appointments[j].Begda;
          obj.end=resultSet[i].appointments[j].Endda;
          obj.title=resultSet[i].RowId;
          obj.type="Type02";
          obj.tentative=false;
          itemsArr.push(obj);
      }
      itm.appointments = itemsArr;  // Added
      
      headArr.push(itm);
  }
 
  
  payloadTest.people = headArr;
  /*payloadTest.people.appointments = itemsArr;*/  // Removed
  
  console.log(headArr);

Hope it helps.

3 Comments

Because there is only one itemsArr, and it is defined outside the loop, each person will have a reference to the same itemsArr list (even though it gets updated on each loop). For each person to have a unique list, you would need to create a new list inside each iteration (and likely have a nested loop to put more than one elem in it).
@user3496976 can you please give use the test content of resultSet?
Thank you Nirali. It is working like a charm. Thank you very much for your quick response

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.