Есть массив с данными, но иногда некоторые данные отсутствуют:
"statistics": [
{
"type": "Ball Possession",
"home": "66%",
"away": "34%"
},
{
"type": "Goal Attempts",
"home": "2",
"away": "1"
},
{
"type": "Shots on Goal",
"home": "0",
"away": "0"
},
{
"type": "Shots off Goal",
"home": "2",
"away": "1"
},
{
"type": "Free Kicks",
"home": "2",
"away": "2"
},
{
"type": "Corner Kicks",
"home": "0",
"away": "0"
},
{
"type": "Throw-in",
"home": "3",
"away": "1"
},
{
"type": "Goalkeeper Saves",
"home": "0",
"away": "0"
},
{
"type": "Attacks",
"home": "14",
"away": "8"
},
{
"type": "Dangerous Attacks",
"home": "11",
"away": "4"
}
]
},
С помощью такого кода обращаюсь к объектам по значению ключа type:
let yellowCard = game.statistics.find(item => item.type === "Yellow Cards");
let attacks = game.statistics.find(item => item.type === "Attacks");
let dangerousAttacks = game.statistics.find(item => item.type === "Dangerous Attacks");
var obj = {
league_name: game.league_name,
league_round: game.league_round,
home_team: game.event_home_team,
away_team: game.event_away_team,
yellow_card_home: yellowCard.home,
yellow_card_away: yellowCard.away,
attacks_home: attacks.home,
attacks_away: attacks.away,
dangerousAttacks_home: dangerousAttacks.home,
dangerousAttacks_away: dangerousAttacks.away,
}
file.games.push(obj)
и после формирую новый объект и добавляю в новый массив. Как сделать проверку для этих значений? Сейчас если их нет в изначальном массиве, то ломается работа скрипта, а нужно добавлять пустое значение или 0.