AngularJS - Factory to hold an Array

How can I update this code to  hold an array of   values.

I want to hold FIELDNAME  and the VALUE

I want to set / add to the list by doing the following - add a value to the array/list.
       

   userFilters.setData(' lastname', 'smith');
   userFilters.setData(' firstname', 'bob');
   userFilters.setData(' Mi', 'D');

And have the object hold an Array of

'lastname','smith'
'firstname','bob'
'mi','D'





App.factory('userFilters', [function () {

    var data = {};

    var getData = function (field) {
        return data[field];
    };

    var setData = function (field, value) {
        data[field] = value;
    };

    return {
        getData: getData,
        setData: setData
    }
}]);
LVL 1
JElsterAsked:
Who is Participating?
 
Alexandre SimõesManager / Technology SpecialistCommented:
Can you give me an example of how you want to use the loop?
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Still I think this is not the right approach.
How are you planning to get that data? You still need an ID right?

userFilters.setData(1, { lastname: 'smith', firstname: 'bob', mi: 'D' });
userFilters.setData(2, { lastname: 'doe', firstname: 'john', mi: 'E' });

Open in new window


To get the last name of the user 2, you'll do:
userFilters.getData(2).lastname;

Open in new window

0
 
JElsterAuthor Commented:
I just want to set the values and loop through the array

I want to set like this

   userFilters.setData(' lastname', 'smith');
   userFilters.setData(' firstname', 'bob');
   userFilters.setData(' Mi', 'D');
0
 
JElsterAuthor Commented:
See my next question.. thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.