-3

Small task, tried so much I am not able to do this.

export class EmployeeDetails {
        employee_id: number,
        employee_name: string,
        employee_startDate: string,
        employee_endDate: string,
        employee_startTime: string,
        employee_endTime: string,
        employee_city: string,
        employee_country: string,
        employee_phone: number,
        employee_email: string

    }

queryResponse = [
{ 
    startDate: '01/01/2001’, 
    endDate: '02/02/2002’ , 
    personDetails : [ 
     { id: 1, name: ‘mounika’, timeDetails: [{ startTime: ’9:00am’, endTime: ’4:00pm’, email: ‘mounika@gmail.com’, phone: ’9232323223’}, { startTime: ’10:00am’, endTime: ’5:00pm’, email: ‘mounika@gmail.com’, phone: ’9232323223’ }]}, 
     { id: 2, name: ‘ashish’, timeDetails: [{ startTime: ’10:00am’, endTime: ’6:00pm’, email: ‘ashish@gmail.com’, phone: ’93647626342’}, { startTime: ’11:00am’, endTime: ’7:00pm’, email: ‘ashish@gmail.com’, phone: ’93647626342’ }] } , 
     { id: 3, name: ‘sai’,  timeDetails: [{ startTime: ’7:00am’, endTime: ’3:00pm’, email: ‘sai@gmail.com’, phone: ’9746236547’}, { startTime: ’8:00am’, endTime: ’5:00pm’, email: ‘sai@gmail.com’, phone: ’9746236547’ }] }  ],   
    locationDetails : [{city: ‘Hyderabad’, country: ‘India’}, {city: ‘Chennai’, country: ‘India’}, {city: ‘Mumbai’, country: ‘India’} ]}

Looking for output like this. And also response is array of more objects that way (I mean response length is greater than 1). How can I have below output with multiple responses?

 data : Array<EmployeeDetails> = [];
    data = [ 
    { employee_id: 1, employee_name: ‘mounika’, employee_startDate: '01/01/2001’, employee_endDate: '02/02/2002’ , employee_startTime: ’10:00am’, endTime: ’5:00pm’, employee_email: ‘mounika@gmail.com’, employee_phone: ’9232323223’, employee_city: ‘Mumbai’, employee_country: ‘India’},  
    { employee_id: 2, employee_name: ‘ashish’, employee_startDate: '01/01/2001’, employee_endDate: '02/02/2002’ , employee_startTime: ’11:00am’, employee_endTime: ’7:00pm’, employee_email: ‘ashish@gmail.com’, employee_phone: ’93647626342’, employee_city: ‘Mumbai’, employee_country: ‘India’},
    { employee_id: 3, employee_name: ‘sai’, employee_startDate: '01/01/2001’, employee_endDate: '02/02/2002’ , employee_startTime: ’8:00am’, employee_endTime: ’5:00pm’, employee_email: ‘sai@gmail.com’, employee_phone: ’9746236547’,employee_city: ‘Mumbai’, employee_country: ‘India’}
    ]
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    What have you tried? This looks like a simple `for` loop over `queryResponse.personDetails`, or you can call `queryResponse.personDetails.map()`. – Máté Safranka Apr 07 '18 at 18:43
  • Can you please help me with code if you don't mind – vishali shetty Apr 07 '18 at 18:51
  • 1
    Possible duplicate of [Parse JSON in JavaScript?](https://stackoverflow.com/questions/4935632/parse-json-in-javascript) – Aluan Haddad Apr 07 '18 at 20:46
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 07 '18 at 21:57

1 Answers1

2

Try to transform your array in new with the help of Array.prototype.map()

var data = queryResponse.personDetails.map(info => {
  return {employee_id: info.id, employee_name: info.name}
});

And you can transform other keys like that.

If you want to add Countries and cities you should use the second arguement in map "id", example:

var data = queryResponse.personDetails.map((info, id) => ({
      employee_id: info.id, 
      employee_name: info.name, 
      employee_city: queryResponse.locationDetails[id].city,       
      employee_country: queryResponse.locationDetails[id].country
    })
 );
Artem
  • 71
  • 4