我有一个这样的数组结构。
[
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
]
我需要像下面这样的 $scope 来进行自动完成搜索。
$scope.name=["john","Gerold","Stuart"];
$scope.city=["NY","LA","Boston"];
任何人都可以使用 angularjs Controller 帮助获得它。 提前致谢!
最佳答案
使用 map
$scope.users = [
{
"id": "1",
"name": "John",
"city": "NY"
},
{
"id": "2",
"name": "Gerold",
"city": "LA"
},
{
"id": "3",
"name": "Stuart",
"city": "Boston"
}
];
$scope.cities = $scope.users.map(function(obj){
return obj.city;
});
console.log($scope.cities);
关于自动完成所需的 AngularJS 多个范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016988/