0

I need to add some value to some "array" but I want that if for example this array has already the value which I want to insert to get some exception,which object or "special array" I can use in java script in node

2
  • 2
    You have some code to share with us? Commented Jul 12, 2015 at 13:44
  • Is there a reason you can't just use a helper function that runs an indexOf check and throws the error manually? Commented Jul 12, 2015 at 13:59

2 Answers 2

1

I guess you want something like this:

var dataStore = {
  _dataStore: {},
  add: function(key, data) {
    if (this._dataStore[key]) {
      throw new Error('already exist');
    }
    this._dataStore[key] = data;
  },
  get: function(key) {
    return this._dataStore[key];
  }
};
//usage
dataStore.add('some-key', 'test');
//will throw exception
//dataStore.add('some-key', 'test');

alert(dataStore.get('some-key'));

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

Comments

0

Best would be to use objects. Also good library to use for this kind of stuff is lodash.

Comments

Your Answer

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