2017-08-01 5 views
0

私は自分の関数に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); 
    } 
} 

//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 
    } 

をしてここに呼ばれる::

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

と、この1はここに呼ばれ

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に)私は私のgetPersonById関数に所有者/ person.idを使用する理由です。

私は私のコンソールログにPERSONID

に取り付けた人物の名前を取得したいと思い、それはまた、[オブジェクトのオブジェクト]ない人の名前... (予定のセルに同じこと言います)

私の間違いがどこにあるか教えてください。

非常

+0

console.log(データ)を実行すると、givオブジェクトオブジェクト – error404

+0

'nameOfPerson'メソッドの中で、配列の最初のオブジェクトに戻ります。そこに間違いがありますか?あなたはアレイ全体を決して通過しないようです。また、 'getPersonNamebyId'メソッドをどのように呼び出すのかを投稿できますか?その人の名前はどこに見えますか? –

+0

あなたの助けを借りてお礼を編集しました – moonshine

答えて

0

は、私の知る限り、あなたは実際にはこれをしたいのおかげ:

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 yeah right、edited。 –

+0

お返事ありがとうございますが、私はあなたの答えを理解できませんでした。なぜvar {firstname、lastName、id}を使ったのですか?私はすでに他の関数でそれを解析しました。 personId(私の関数のパラメータ)が配列のpersonIdと等しく、添付されたpersonNameを返す場合は、解析された配列を調べたいと思います。 – moonshine

関連する問題