我如何从一个数组中检索对象来创建一个新的数组?

问题描述 投票:1回答:1

我有一个数组,看起来像这样。

0:{9 items
"id":"380721"
"group":""
"po_type":NULL
"team1short":"TOR"
"team1long":"Toronto"
"team2short":"OTT"
"team2long":"Ottawa"
"date":"2019-10-03 01:00:00"
"score":{...}3 items
}
1:{9 items
"id":"380731"
"group":""
"po_type":NULL
"team1short":"STL"
"team1long":"St. Louis"
"team2short":"WSH"
"team2long":"Washington"
"date":"2019-10-03 02:00:00"
"score":{...}3 items
}
2:{9 items
"id":"380741"
"group":""
"po_type":NULL
"team1short":"EDM"
"team1long":"Edmonton"
"team2short":"VAN"
"team2long":"Vancouver"
"date":"2019-10-03 04:00:00"
"score":{...}3 items
}
3:{9 items
"id":"380751"
"group":""
"po_type":NULL
"team1short":"VGK"
"team1long":"Vegas"
"team2short":"SJS"
"team2long":"San Jose"
"date":"2019-10-03 04:30:00"
"score":{...}3 items
}
4:{9 items
"id":"380761"
"group":""
"po_type":NULL
"team1short":"TBL"
"team1long":"Tampa Bay"
"team2short":"FLA"
"team2long":"Florida"
"date":"2019-10-04 01:00:00"
"score":{...}3 items
}

我是新手。 这个数组持续了一个冰球赛季的时间。 我不知道该如何创建一个新的数组,只包含team1long=多伦多的比赛。 你可以在 games[0] 中找到这个。

任何帮助都会让我不再无情地用头撞墙!这是我用来循环所有游戏的代码。

这是我用来循环所有游戏的代码。 javascript

<% for (i = 0; i <games1.games.length; i++) { %>
<tr>
    <th scope="row"><%= games1.games[i].date %></th>
        <th><%= games1.games[i].team1long %></th>
        <td><%= games1.games[i].score.goals1 %></td>
        <td><%= games1.games[i].team2long %></td>
        <td><%= games1.games[i].score.goals2 %></td>
</tr>   
<% } %>

这工作就很好。

我试图用这样的东西来创建一个新的arry。

game[] gamesFromToronto;

For(i=0 ; i < games.Length ; i++)
{
 if(games[i].team1Long == “Toronto”)
{
gamesFromToronto[gamesFromToronto.length] = games[i];
}
}
arrays new-operator
1个回答
0
投票

审阅了你的代码,逻辑是正确的,应该可以用。但是......你在你的代码中犯了很多错误(语法错误和拼写代码错误),比如你用大写字母代替小写字母开始了很多事情,比如......。

For --> should be --> for
team1Long  --> should be --> team1long  ( l and not L ) 
“Toronto” --> should be --> "Toronto" ( " and not “ ) 
gamesFromToronto[gamesFromToronto.length] = games[i];  --> should be --> gamesFromToronto.push(games[i]);

所以这里有一个正确的工作代码。

//solution 
var gamesFromToronto = [];

for (i=0 ; i < games.length ; i++ ) {
     if(games[i].team1long == "Toronto"){
        gamesFromToronto.push(games[i]);
    }
}

对于测试:

//data sample
 games = [
{
"id":"380721",
"group":"",
"po_type":null,
"team1short":"TOR",
"team1long":"Toronto",
"team2short":"OTT",
"team2long":"Ottawa",
"date":"2019-10-03 01:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
},
{
"id":"380741",
"group":"",
"po_type":null,
"team1short":"EDM",
"team1long":"Toronto",
"team2short":"VAN",
"team2long":"Vancouver",
"date":"2019-10-03 04:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
}] ; 

//solution 
var gamesFromToronto = [];

for (i=0 ; i < games.length ; i++ ) {
	 if(games[i].team1long == "Toronto"){
		gamesFromToronto.push(games[i]);
	}
}
//visualisation
document.getElementById("demo").innerHTML = JSON.stringify(gamesFromToronto);
<p id="demo"></p>
© www.soinside.com 2019 - 2024. All rights reserved.