我遇到了如何在数组中循环和存储的问题。
<a id="reply" onclick='loginmodal("#collapseBox{{ $comment->id }}", {{ Auth::user() ? true : false }})' role="button">Reply</a>
对于 /** Model to display response*/
export class ResultsData {
id: number,
name: string,
startDate: string,
endDarte: string,
startTime: string,
endTime: string,
city: string,
country: string
}
/**Response upon service call*/
response = [
{
startDate: '04/05/2018',
endDate: '05/05/2018' ,
personDetails : [ { id: 5, name: 'kumar', timeDetails: [{ startTime: ’09:22pm’, endTime: ’08:33’}, { startTime: ’01:22pm’, endTime: ’01:33’}] }, { id: 4, name: 'vishal', timeDetails: [{ startTime: ’09:22pm’, endTime: ’08:33’}, { startTime: ’01:22pm’, endTime: ’01:33’}] } , { id: 2, name: 'dinesh', timeDetails: [{ startTime: ’09:22pm’, endTime: ’08:33’}, { startTime: ’01:22pm’, endTime: ’01:33’}] }],
locationDetails : [{city: 'new york', country: 'us'}, {city: 'los angeles', country: 'us'} ]}
]
/**Component.ts*/
tableData : Array<ResultsData> = []
中的每个人,我想将他的详细信息(EG。名称和ID)和时间详细信息的最后一个索引,位置详细信息的最后一个索引推送到数组中。
personDetails
答案 0 :(得分:0)
我建议您尝试使用构造函数方法将personDetails
映射到tableData
:
class ResultsData {
id: number;
name: string;
startDate: string;
endDate: string;
startTime: string;
endTime: string;
city: string;
country: string;
constructor(item) {
Object.assign(this, item);
}
}
tableData = response[0].personDetails.map(item => new ResultsData ({
id: Number(item.id),
name: item.name,
startDate: response[0].startDate,
endDate: response[0].endDate,
startTime: item.timeDetails[item.timeDetails.length - 1].startTime,
endTime: item.timeDetails[item.timeDetails.length - 1].endTime,
city: response[0].locationDetails[response[0].locationDetails.length - 1].city,
country: response[0].locationDetails[response[0].locationDetails.length - 1].country
}));