0

I am new to javascript. i want to parse this response .

var date=[];
   var details=[];
    for(post in resulttable.posts){
      date=date.concat(resulttable.posts[post].days.Date);
      details= details.concat(resulttable.posts[post].days.details);
    }

I dont know where am missing. please help me , I want those details in one array and dates to be another array.

{
    "status": 1,
    "count": 2,
    "posts": [{
        "days": {
            "details": [{
                "place": "labs",
                "StartTime": "01:00:00",
                "EndTime": "02:00:00",
                "Description": "Meeting with team",
                "participants": [{
                    "Name": "KK",
                    "Designation": "VP, Operations",
                    "ContactNumber": "111"
                }, {
                    "Name": "MN1",
                    "Designation": "Project Lead",
                    "ContactNumber": "111"
                }]
            }],
            "Date": ["2017-02-02"]
        },
        "name": "test"
    }, {
        "days": {
            "details": [{
                "place": "India",
                "StartTime": "01:00:00",
                "EndTime": "03:00:00",
                "Description": "Agenda1",
                "participants": [{
                    "Name": "Kk",
                    "Designation": "VP, Operations",
                    "ContactNumber": "11111"
                }, {
                    "Name": "MN",
                    "Designation": "Project Lead",
                    "ContactNumber": "111"
                }]
            }, {
                "place": "microsoft",
                "StartTime": "01:00:00",
                "EndTime": "02:00:00",
                "Description": "Meet CEO",
                "participants": [{
                    "Name": "VR",
                    "Designation": "Project Lead",
                    "ContactNumber": "111"
                }]
            }, {
                "place": "microsoft",
                "StartTime": "01:00:00",
                "EndTime": "02:00:00",
                "Description": "Meet CEO",
                "participants": [{
                    "Name": " VR",
                    "Designation": "Project Lead",
                    "ContactNumber": "111"
                }]
            }, {
                "place": "Formule",
                "StartTime": "10:50:00",
                "EndTime": "11:50:00",
                "Description": "Meet Rajesh",
                "participants": [{
                    "Name": "MN",
                    "Designation": "Project Lead",
                    "ContactNumber": "111"
                }]
            }, {
                "place": "Dell",
                "StartTime": "04:00:00",
                "EndTime": "08:00:00",
                "Description": "Agenda 2",
                "participants": [{
                    "Name": "MN",
                    "Designation": "Project Lead",
                    "ContactNumber": "1111111"
                }]
            }],
            "Date": ["2017-02-03"]
        },
        "name": "test"
    }]
}
Andreas
  • 21,535
  • 7
  • 47
  • 56
SreeAndroidDev
  • 141
  • 1
  • 1
  • 11
  • [`for...in...`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) is for objects: "_iterates over the enumerable properties of an object, **in arbitrary order**._". `posts` is an array, hence you should be using [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) (Why? -> [Why is using “for…in” with array iteration a bad idea?](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Andreas Feb 08 '17 at 09:32
  • try `of` instead of `in` – A. L Feb 08 '17 at 09:36

1 Answers1

0

Check this fiddle

var details = new Array();
var dates = new Array();
for (var i = 0; i < resulttable.posts.length; i++) {
  dates = dates.concat(resulttable.posts[i].days.Date);
  details = details.concat(resulttable.posts[i].days.details);
}
console.log(details);
console.log(dates);

Or

var details = new Array();
var dates = new Array();
for (post of resulttable.posts) {
  dates = dates.concat(post.days.Date);
  details = details.concat(post.days.details);
}
console.log(details);
console.log(dates);
Harshal
  • 961
  • 1
  • 11
  • 26
  • Thanks. How about adding a function for participants in details object. like how we use object class for arraylist in java! – SreeAndroidDev Feb 08 '17 at 10:14