如何在一个公共变量中从多个数组中获取名称ID?

时间:2016-02-17 12:44:58

标签: javascript arrays angularjs multidimensional-array local-storage

这是我的本地存储阵列,我想要" 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;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

这对于Angular来说并不是特别的。您只需要Array.prototype.map

&#13;
&#13;
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;
&#13;
&#13;

但是,这里有一些Angular建议:不要在控制器中使用$( "#tags" ).autocomplete,为这个DOM创建专用指令,控制器不适合这个。