Accéder aux éléments d'un tableau associatif
Bonjour,
Je cherche à faire un service angular pour stocker des données.
Mon idée est d'utiliser un tableau de tableau associatif et de récupérer ces valeurs via la clé.
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| .factory('storeService', function(){
// create empty
function storeService(){
this.store = [];
}
// use this to register new store from a controller, other factory, wherever.
storeService.prototype.addStore = function(st){
this.store.push(st);
}
// find value
storeService.prototype.findById = function(id){
// for all item in the table
for(var ind in this.store){
// check if the item has the key requested
if(this.store[ind].hasOwnProperty(id))
{
console.log('us ', this.store[ind][id]);
return this.store[ind][id];
}
}
return null;
}
return new storeService();
}) |
Sauf que ma récupération ne fonctionne pas.
Voici la forme de mon objet :
Code:
this.store = [{id: 'user1', 'value': { 'id': '11060', 'username': "jctoto", 'email': "jctoto@toto.net"}}];
Une idée du problème ?
Merci.