这是我的本地存储阵列,我想要" name"来自所有数组的id ..如何使用角度js或js得到它?
[{"email":"John@a.com","pass":"Doe","name":"John" },
{"email":"Anna@a.com","pass":"Smith","name":"Smith"},
{"email":"Peter@a.com","pass":"Jones","name":"Jones"}]
var app = angular.module('app',['ngStorage']);
app.controller('ctrl',function($scope){
$scope.complete=function(){
debugger
localStorage["data"] = localStorage.getItem('person');
$scope.obj = JSON.parse(localStorage["data"]);
console.log($scope.obj['0'].name);
$( "#tags" ).autocomplete({
source: $scope.obj['0'].name
});
}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
&#13;
答案 0 :(得分:2)
这对于Angular来说并不是特别的。您只需要Array.prototype.map:
var data = [{"email":"John@a.com","pass":"Doe","name":"John" },
{"email":"Anna@a.com","pass":"Smith","name":"Smith"},
{"email":"Peter@a.com","pass":"Jones","name":"Jones"}];
var names = data.map(function(user) {
return user.name;
});
document.write(JSON.stringify(names, null, 4))
&#13;
但是,这里有一些Angular建议:不要在控制器中使用$( "#tags" ).autocomplete
,为这个DOM创建专用指令,控制器不适合这个。