gpt4 book ai didi

javascript - 如何将 'n' 对象转换为数组

转载 作者:行者123 更新时间:2023-12-03 02:58:44 26 4
gpt4 key购买 nike

在对 api 响应和今天的日期进行比较后,我得到了一些对象:

function taffy() {
var d = new Date();
$scope.day = d.getDate();
$scope.month = d.getMonth() + 1;
$scope.year = d.getFullYear();
$scope.today = $scope.year + "-" + $scope.month + "-" + $scope.day + "T00:00:00";
}

function getBackAll() {
$http.get('api/Invoice?')
.then(function(data) {
$scope.amadeus = data.data.Response;

for (var i = 0; i < $scope.amadeus.length; i++) {
if ($scope.amadeus[i].ProgramPayDate === $scope.today && $scope.amadeus[i].StatusId === 3) {
$scope.viroba = $scope.amadeus[i];
//console.log($scope.viroba);
}
};
//console.log($scope.amadeus);
});
}

响应是“n”个对象。我想做的是在我的 html View 上显示信息,但我不能。之前我问过,有人告诉我需要将对象转换为数组,但是我在互联网上找到的内容对我来说不是很有用。

假设我的控制台上有下一个响应:

{id: 1, name: "Arthur", nickname: "mercury"}
{id: 2, name: "Chuck", nickname: "lemmy"}

我该如何获取信息并将其显示在 html View 的表格中?

<table align="center">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Nickname</th>
</tr>
</thead>
<tbody>
<tr>
<th>{{id}}</th>
<th>{{name}}</th>
<th>{{nickname}}</th>
</tr>
</tbody>
</table>

如果我只得到一个值,它显示没有问题。当我有多个时就会出现问题。

有人可以帮助我吗?

提前致谢。

最佳答案

您可以将对象列表存储在一个数组中,现在我们假设该数组是users用户看起来像这样

users = [{id: 1, name: "Arthur", nickname: "mercury"},
{id: 2, name: "Chuck", nickname: "lemmy"}]

在您的代码中,尝试将 amadeus[i] 填充到 users 数组中,如下所示

function getBackAll() {
$scope.users = []; // Or initialise this wherever logical
$http.get('api/Invoice?')
.then(function(data) {
$scope.amadeus = data.data.Response;

for (var i = 0; i < $scope.amadeus.length; i++) {
if ($scope.amadeus[i].ProgramPayDate === $scope.today && $scope.amadeus[i].StatusId === 3) {
$scope.users.push($scope.amadeus[i]);
//console.log($scope.viroba);
}
};
//console.log($scope.amadeus);
});
}

您可以使用 ngRepeat 迭代您在 Controller 中填充的 users 数组。 https://docs.angularjs.org/api/ng/directive/ngRepeat

<table align="center">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Nickname</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<th>{{user.id}}</th>
<th>{{user.name}}</th>
<th>{{user.nickname}}</th>
</tr>
</tbody>
</table>

关于javascript - 如何将 'n' 对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47505513/

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