Из массива объектов с помощью цикла в другой массив объектов записать определенные ключи и их значения
// examples
// [
// { name: 'Jason Mount', team: 'Manchester United', goals: 4 },
// { name: 'Jason Mount, team: 'Manchester United', goals: 0 },
// { name: 'Finne Bard', team: 'Fulham United', goals: 1 },
// { name: 'Gerhardt Yannick', team: 'Liverpool', goals: 8 },
// ];
const players = [
{
name: 'Jason Mount',
birthdate: '19.12.1993',
country: 'Deutschland',
number: '21',
team: 'Manchester United',
position: 'MF',
goals: 4,
},
{
name: 'Jason Mount',
birthdate: '01.01.2001',
country: 'Deutschland',
number: '16',
team: 'Manchester United',
position: 'MF',
goals: 0,
},
{
name: 'Finne Bard',
birthdate: '13.02.1995',
country: 'Norwegen',
number: '26',
position: 'FW',
team: 'Fulham United',
goals: 1,
},
{
name: 'Gerhardt Yannick',
birthdate: '13.03.1994',
country: 'Deutschland',
number: 31,
position: 'MF',
team: 'Liverpool',
goals: 8,
},
];
>Вот мой пример, но выходит не то что нужно.
//
function getGoalsStat(players) {
let arr = [];
let obj = {};
for (let item in players) {
arr.push(obj.name = players[item].name);
arr.push(obj.team = players[item].team);
arr.push(obj.goals = players[item].goals);
}
return arr;
};
Ответы (1 шт):
Автор решения: Алексей Шиманский
→ Ссылка
const players = [
{
name: 'Jason Mount',
birthdate: '19.12.1993',
country: 'Deutschland',
number: '21',
team: 'Manchester United',
position: 'MF',
goals: 4,
},
{
name: 'Jason Mount',
birthdate: '01.01.2001',
country: 'Deutschland',
number: '16',
team: 'Manchester United',
position: 'MF',
goals: 0,
},
{
name: 'Finne Bard',
birthdate: '13.02.1995',
country: 'Norwegen',
number: '26',
position: 'FW',
team: 'Fulham United',
goals: 1,
},
{
name: 'Gerhardt Yannick',
birthdate: '13.03.1994',
country: 'Deutschland',
number: 31,
position: 'MF',
team: 'Liverpool',
goals: 8,
},
];
function getGoalsStat(players) {
let arr = [];
for (let player of players) {
arr.push({ name: player.name, team: player.team, goals: player.goals });
}
return arr;
};
console.log(getGoalsStat(players));
Упрощённо:
const players = [
{
name: 'Jason Mount',
birthdate: '19.12.1993',
country: 'Deutschland',
number: '21',
team: 'Manchester United',
position: 'MF',
goals: 4,
},
{
name: 'Jason Mount',
birthdate: '01.01.2001',
country: 'Deutschland',
number: '16',
team: 'Manchester United',
position: 'MF',
goals: 0,
},
{
name: 'Finne Bard',
birthdate: '13.02.1995',
country: 'Norwegen',
number: '26',
position: 'FW',
team: 'Fulham United',
goals: 1,
},
{
name: 'Gerhardt Yannick',
birthdate: '13.03.1994',
country: 'Deutschland',
number: 31,
position: 'MF',
team: 'Liverpool',
goals: 8,
},
];
let getGoalsStat = (players) => players.map(player => ({ name: player.name, team: player.team, goals: player.goals }));
console.log(getGoalsStat(players));