user3844586
user3844586

Reputation: 151

How to get a name id from multiple array in one common variable?

This is my local storage array and i want "name" id from all they array .. how can i get it using angular js or js ?

[{"email":"[email protected]","pass":"Doe","name":"John" },
{"email":"[email protected]","pass":"Smith","name":"Smith"},
{"email":"[email protected]","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>

Upvotes: 1

Views: 47

Answers (1)

dfsq
dfsq

Reputation: 193271

This is not really anything specific for Angular. You just need Array.prototype.map:

var data = [{"email":"[email protected]","pass":"Doe","name":"John" },
{"email":"[email protected]","pass":"Smith","name":"Smith"},
{"email":"[email protected]","pass":"Jones","name":"Jones"}];

var names = data.map(function(user) {
    return user.name;
});

document.write(JSON.stringify(names, null, 4))

However, here is piece of Angular advice: don't use $( "#tags" ).autocomplete in controller, create dedicated directive for this DOM stuff, controller is not the place for this.

Upvotes: 2

Related Questions