【发布时间】:2018-09-17 03:17:27
【问题描述】:
我对如何在数组中循环和存储有疑问。
/** 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> = []
对于personDetails 中的每个人,我想将他的详细信息(例如姓名和 ID)和时间详细信息的最后索引、位置详细信息的最后索引推送到数组中。
Expected output:
tableData = [
{ id: 5, name: 'kumar', startDate: '04/05/2018', endDate: '05/05/2018', startTime: ’01:22pm’, endTime: ’01:33’ city: 'los angeles', country: 'us'},
{ id: 4, name: 'vishal', startDate: '04/05/2018', endDate: '05/05/2018', startTime: ’01:22pm’, endTime: ’01:33’, city: 'los angeles', country: 'us'},
{ id: 2, name: 'dinesh', startDate: '04/05/2018', endDate: '05/05/2018', startTime: ’01:22pm’, endTime: ’01:33’, city: 'los angeles', country: 'us'}
]
【问题讨论】:
-
locationDetails是一个数组,如果有多个元素会怎样? -
它应该获取每个人的时间和位置详细信息的最后一个索引并推送。我做了一些细微的改变,即也添加了时间细节。我无法弄清楚我该怎么做。
-
请再次查看示例,
timeDetails出现insidepersonDetails,示例输出中没有“纽约”的痕迹。 -
是的,我的错我只有那种 json 里面的时间细节和纽约,即我只想要位置数组和时间细节数组的最后一个索引。
-
@tevemadar 抱歉是我的错误有一点变化,每个人都有时间详细信息属性。你能帮帮我吗
标签: javascript arrays angular typescript lodash