0

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
    }
}]);
1
  • data = [], data.push(field); data.push(value) Commented Mar 4, 2015 at 17:27

2 Answers 2

2

You could recreate this fairly simply without any need for the methods you are creating.

Javascript Objects are designed to do exactly what you are looking for here.

App.factory('userFilters', function() {
  return {};
});

Rather than using a getter and setter, you could instead get and set values with square bracket accessors.

// setting properties
userFilters['lastname'] = 'smith';
userFilters['firstname'] = 'bob';
userFilters['Mi'] = 'D';

// getting properties
userFilters['lastname']; // 'smith'
userFilters['firstname']; // 'smith'

If you want to be able to have full control of what happens at get/set time, you could look at intercepting these calls with internal getters and setters, providing you know the property names before hand.

Finally, you could also wrap your own get and set functions around the object in order to hide it. However, this would make more sense as a Service.

App.service('userFilters', function() {
  var store = {};

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

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

If it's important that your factory/service exposes an array then I would recommend sticking to using an object to store keys and values, but adding an array export method.

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;
  };
});

If you want to loop through the properties, you can use a for-in loop.

for(var key in userFilters) {
  var value = userFilters[key];
  console.log(key, value);
}

You can also check whether there any keys at all, using the Object.keys method.

Object.keys(userFilters);  // ['lastname', 'firstname', 'Mi']

This will return an array of all the keys in the object. If it has length 0, then you know it's empty.

Sign up to request clarification or add additional context in comments.

2 Comments

userFilters['lastname'] = 'smith'; userFilters['firstname'] = 'bob'; userFilters['Mi'] = 'D';
How can I check if the object has values? And loop through them?
1

In order to use an array for storage you will need to update both getData and setData methods and define data as an array []:

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

    var data = [];

    var getData = function (field) {
        for(var i=0; i<data.length; i+=2) {
            if(data[i] == field) {
                return data[i+1];
            }
        }

        return null;
    };

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

    return {
        getData: getData,
        setData: setData
    }

}]);

1 Comment

this doesn't work userFilters.setdata('a','b'); thx again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.