[英]JS array with key in node
我需要在“数组”中添加一些值,但是我想,例如,如果该数组已经具有要插入以获取某些异常的值,则可以在节点的Java脚本中使用哪个对象或“特殊数组”
我想你想要这样的东西:
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'));
最好是使用对象。 lodash也是用于这种东西的很好的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.