AngularJS - why doesn't this work - use object to store array values

Can you please tell me why this does not work.

HERE'S THE OBJECT


// Holds User selected fields & filter values
App.service('userFilters', function () {
    var store = {};

    this.toArray = function () {
        var records = [];
        return Object.keys(store).map(function (key) {
            records.push([key, store[key]]);
        });
    };

    this.get = function (key) {
        return store[key];
    };

    this.set = function (key, value) {
        store[key] = value;
    };
});


// HERE'S THE CODE TO USE IT

      userFilters.set('DD','XX');
         userFilters.set('a', 'b');
         userFilters.set('c', 'bdddddd');


// gET THE DATA

  var uFilters = userFilters.toArray();

            if (uFilters.length > 0) {
             

                for (p = 0; p < uFilters.length; p++) {

                    var x = uFilters[p];
                }
}
LVL 1
JElsterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Ok mate, I still don't fully understand what you're trying to achieve mainly because the way your setting the values isn't compatible with the way you're accessing them.

Lets see, when you set the value you're passing a key and a value.
When you're accessing what are you trying to achieve?

The closest thing I can think here if to add a method in the factory that gives you all the items so that you can loop through them. Something like:
App.service('userFilters', function () {
    var store = {};

    this.get = function (key) {
        return store[key];
    };

    this.set = function (key, value) {
        store[key] = value;
    };

    this.getAll = function () {
        var result = [];

        for (var i in store) {
            var item = {};
            item[i] = store[i];
            result.push(item);
        }
        
        return store;
    };
});

Open in new window

This will give you a collection of objects that you can use in your loop as:
for (p = 0; p < userFilters.getAll().length; p++) {
    var x = userFilters[p];
}

Open in new window

But it's a mess because you don't know what's inside each object item...
That's why I don't understand what's your goal inside the loop.
JElsterAuthor Commented:
I just trying to store an array of values.. thx again!

This doesn't seem to work.. does not loop

for (p = 0; p < userFilters.getAll().length; p++) {
    var x = userFilters[p];
}
Alexandre SimõesManager / Technology SpecialistCommented:
Ah sorry, there's an error in the code.
The return line was wrong.
App.service('userFilters', function () {
    var store = {};

    this.get = function (key) {
        return store[key];
    };

    this.set = function (key, value) {
        store[key] = value;
    };

    this.getAll = function () {
        var result = [];

        for (var i in store) {
            var item = {};
            item[i] = store[i];
            result.push(item);
        }
        
        return result;
    };
});

Open in new window


This will give you a list of objects with key/value.
Something like: [{ DD: 'XX' }, { a: 'b' }, { c: 'bdddddd' }]

An array of values like you say would be more like: [ 'XX', 'b', 'bdddddd' ]

Which version are you actually looking for?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.