简体   繁体   中英

JS array with key in node

我需要在“数组”中添加一些值,但是我想,例如,如果该数组已经具有要插入以获取某些异常的值,则可以在节点的Java脚本中使用哪个对象或“特殊数组”

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM