次について質問があります。list1 から list2 を作成する必要があるため、解決策 1 を適用しましたが、うまくいきました。ただし、単体テスト用に別の機能にする必要がありました。それを変更した後、解決策2のようにすることができませんでした。戻り値を出力すると、配列の3つの要素に対して未定義と表示されます。この問題に対するアドバイスはありますか?私は一生懸命努力しましたが、まだ解決できません。
var list1 = [
{ firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: 'Python' },
{ firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby' }
];
var list2 = [
{ firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java',
greeting: 'Hi Sofia, what do you like the most about Java?'
},
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: 'Python',
greeting: 'Hi Lukas, what do you like the most about Python?'
},
{ firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby',
greeting: 'Hi Madison, what do you like the most about Ruby?'
}
];
解決策 1
let greetings1 = list1.map(person => {
return Object.assign(
{ firstName: person.firstName },
{ lastName: person.lastName },
{ country: person.country },
{ continent: person.continent },
{ age: person.age },
{ language: person.language },
{
greeting: `Hi ${person.firstName}, what do you like the most about ${
person.language
}?`
}
);
});
解決策 2
function greetDevelopers(list1) {
const greetings = list1.map(person => {
Object.assign(
{ firstName: person.firstName },
{ lastName: person.lastName },
{ country: person.country },
{ continent: person.continent },
{ age: person.age },
{ language: person.language },
{
greeting: `Hi ${person.firstName}, what do you like the most about ${
person.language
}?`
}
);
});
return greetings;
}