【问题标题】:Function for looping response循环响应函数
【发布时间】: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 出现inside personDetails,示例输出中没有“纽约”的痕迹。
  • 是的,我的错我只有那种 json 里面的时间细节和纽约,即我只想要位置数组和时间细节数组的最后一个索引。
  • @tevemadar 抱歉是我的错误有一点变化,每个人都有时间详细信息属性。你能帮帮我吗

标签: javascript arrays angular typescript lodash


【解决方案1】:

我建议您尝试将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
}));

【讨论】:

  • 我怎样才能获得每个人的位置和时间详细信息的最后索引。
  • @BhachiKumar 您在 personDetails 数组中有 timeDetails 属性,您认为它是有效的 json 吗?甚至不可能在控制台中运行您的响应示例 (SyntaxError)。我建议您在继续之前查看response 的格式。
  • 抱歉是我的失误有细微的变化,每个人都有时间详情属性。你能帮帮我吗
  • @BhachiKumar 你可以在 stackblitz 或 plunker 或 typescript 操场上签到
  • @dhilt 我得到 startTime 未定义并退出循环并且没有给出任何结果。你能帮帮我吗? TIA
猜你喜欢
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2015-04-19
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2021-07-12
相关资源
最近更新 更多