gpt4 book ai didi

javascript - 只列出对象数组的一个属性

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:04 24 4
gpt4 key购买 nike

我想要一个 Angular 列表,显示一组对象中一个对象属性的所有实例,仅此而已——例如,仅国家/地区。

$scope.testSites = [
{ "site": "Testsite1", "country": "Country1", "customer": "Customer1"},
{ "site": "Testsite2", "country": "Country2", "customer": "Customer2"}
];
$scope.chosenCategory = 1;
$scope.categoryNames = ["site", "country", "customer"];
$scope.aspect = $scope.categoryNames[$scope.chosenCategory];

但是,我想使用上面的变量“方面”来选择要在列表中显示的属性。像 {{x.country}} 这样的东西虽然有效,但还不够。我试过这个,但它返回一个空列表:

<table border="1" class="list-group-item list-group-item-success">
<tr>
<th>{{aspect | capitalize}}</th>
</tr>

<tr ng-repeat="x in testSites | orderBy:myOrderBy">
<td>
{{x.aspect}}
</td>

</tr>
</table>

有什么我可以做的吗?

最佳答案

您可以通过 {{ x[aspect] }} - [] 括号符号来完成。它计算表达式并使用结果来查找属性。

你可以找到Demo here

关于javascript - 只列出对象数组的一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47527920/

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