gpt4 book ai didi

Javascript - 尝试使用属性过滤数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:53:27 25 4
gpt4 key购买 nike

这是我卡住的一些示例代码。我认为这与数组的任何指导属性有关?我希望使用 league 属性过滤数组。

<script>
var teams = [
{name: "newcastle", league: "Premiership"},
{name: "Hull", league: "League 1"},
{name: "Sunderland", league: "League 1"},
{name: "Arsenal", league: "Premiership"}];

var filterLeague = teams.filter(function (teams) {
return (teams.league === "Premiership");
});

document.getElementById("output").innerHTML = filterLeague;

</script>

最佳答案

过滤结果为对象数组。您需要将其隐藏为某种字符串才能显示在 DOM 元素中

<div id='output'></div>
<script>
var teams = [
{name: "newcastle", league: "Premiership"},
{name: "Hull", league: "League 1"},
{name: "Sunderland", league: "League 1"},
{name: "Arsenal", league: "Premiership"}];

var filterLeague = teams.filter(function (teams) {
return (teams.league === "Premiership");
});

document.getElementById("output").innerHTML = JSON.stringify(filterLeague);

</script>

关于Javascript - 尝试使用属性过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47042319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com