gpt4 book ai didi

angularjs - 如何使用 `AngularJS` 将键值对转换为数组列表?

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

这是我们的数据:

$scope.arr = {
"1": "A",
"2": "B",
"3": "C",
"4": "D",
"5": "E",
"6": "F",
"7": "Others",
"fromDate": {
"$date": "2015-02-13T20:59:28.947Z"
}
}

如何将此数据转换为列表/数组格式?

我期待这样的事情:(顺便说一句,我也想删除 fromDate)

$scope.formattedArr = [
{value: "1",name: "A" },
{value: "2",name: "B" },
{value: "3",name: "C" },
{value: "4",name: "D" },
{value: "5",name: "E" },
{value: "6",name: "F" },
{value: "7",name: "Others" }
]

最佳答案

angular.module('app',[])
.controller('ctrl',function($scope){
$scope.arr = {
"1": "A",
"2": "B",
"3": "C",
"4": "D",
"5": "E",
"6": "F",
"7": "Others",
"fromDate": {
"$date": "2015-02-13T20:59:28.947Z"
}
}
$scope.aarr = [];
for (key in $scope.arr){
$scope.aarr.push({'value' : key , 'name' : $scope.arr[key] })
}


console.log($scope.aarr)
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">{{aarr}}</div>

关于angularjs - 如何使用 `AngularJS` 将键值对转换为数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919886/

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