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 Answers
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'));
indexOfcheck and throws the error manually?