返回一个对象的参数

问题描述:

我想给一个函数返回我一个人的名字,当我给我的函数一个id。下面是我做了什么:返回一个对象的参数

nameOfPerson(personjson, personId){ 
    let id = personId; 
    let json = personjson.json(); 
    let data = []; 

    //console.log("id in nameOfPerson method " + id); 
    //console.log("json in nameOfPerson method " + JSON.stringify(json)) 

    for (let person of json){ 
     let newPerson = new Person() 
     newPerson.text = person.firstName + ' ' + person.lastName, 
     newPerson.id = person.id 
     if (id == newPerson.id){ 
     data.push(newPerson.text) 
     } 
     console.log("data on nameOfPerson method " + JSON.stringify(data)) 
     return JSON.stringify(data); 
    } 
} 

这是由这个函数调用:

getPersonNamebyId(personId): Promise<Person[]> { 
    return this.http.get(this.PersonsUrl) 
         .toPromise() 
         .then(response => this.nameOfPerson(response, personId)) 
         .catch(this.handleError) 
} 

而这一次在这里叫:

//parsingFunction 
    parseAppointment(appointmentJson) { 
    let data = new Array(); 
    let json = appointmentJson.json() 
     for (let appointment of json) { 
     let newAppointment = new Appointment() 
     newAppointment.text = appointment.reason +' '+ this.personsService.getPersonNamebyId(appointment.personId), // this.personsService.getPersonNamebyId(appointment.personId) 
     newAppointment.id = appointment.id, 
     newAppointment.ownerId = appointment.personId, 
     newAppointment.startDate = appointment.date, 
     newAppointment.endDate = this.add30mnTo(appointment.date), 
     data.push(newAppointment); 
     } 
     console.log(data) 
     return data 
    } 

这里称为:

getAppointments(): Promise<Appointment[]> { 

    return this.http.get(this.AppointmentUrlGet) 
       .toPromise() 
       .then(response => this.parseAppointment(response)) 
       .catch(this.handleError); 

    } 

整个事情的目标是转换的这一个对象:

[ 
{ 
id: "bc127c74-377b-4ea6-925a-3dd5e0227482", 
reason: "testing", 
date: "2017-06-22T14:59:55.000Z", 
personId: "8090210d-e154-4db0-96c1-42688f45971a" 
}, 
{ 
id: "bc127c74-377b-4ea6-925a-3dd5e0227482", 
reason: "testing02", 
date: "2017-06-22T14:59:55.000Z", 
personId: "8090210d-e154-4db0-96c1-42688f45971a" 
} 
] 
的人(ID = appointment.id,文本=原因+ OWNERNAME)对象的

这就是为什么我使用所有者/ person.id我getPersonById功能。

我想获得连接,将personId

在我的控制台日志的人的名字,它说:[Object对象]的人没有名字...... (也约会的细胞同样的事情)

你能告诉我我的错误在哪里吗?

非常感谢

+0

只是做console.log(数据),那么它不会得到e对象对象 – error404

+0

在'nameOfPerson'方法内部,您返回数组的第一个对象。那里可能有错误吗?你似乎从来没有经历过整个阵列。你可以发布你如何调用'getPersonNamebyId'方法,你期望看到这个人的名字? –

+0

编辑感谢您的帮助 – moonshine

据我所看到的,你其实只是想做到这一点:

var nameOfPerson = (personjson, personId) => { 
    var {firstName,lastName,id} = 
    personjson.json().find(el=>el.id === personId); 

    return firstName && lastName&&id 
    ? { text: firstName+lastName, id } //maybe Object.assign(new Person(),{text: ... }) instead 
    : { id: "unknown"}; 
}; 

使用方法如下:

var {text, id} = nameOfPerson(json,12); 
console.log(" text",text,"id",id); 

或者在一个承诺:

getjson().then(
    json => nameOfPerson(json,12) 
) 
.then(
    ({text, id}) => console.log(text,id) 
); 
+0

看起来他们想'id' – Jamiec

+0

@jamiec是的,编辑。 –

+0

谢谢你的回答,但我不明白你的答案,你能解释一下吗?为什么你使用var {firstname,lastName,id}?我已经在其他函数上解析过它。我想检查解析的数组,如果我的personId(我的函数的参数)等于数组上的personId并返回附加的personName。 – moonshine