简体   繁体   中英

ExtJS 5 - How to convert Object to Collection/MixedCollection/HashMap?

I have an object on which i would like to perform add/remove/find operation. So i'm just trying to convert that Object as HashMap/Collection object using Ext.util.Collection/Ext.util.MixedCollection/Ext.util.HashMap classes. As it is already an Object i just want to perform typecasting? Is there a straight forward option availble in ExtJS to attain this?

Currently using the following utility method i'm attaining this. Is there any other better option available other than this?

Raw Object:-

  Object {abbreviateValues: true, category: "P", charCode: 13092,charDescription: Array[5], charId: 13092…}

Utility Method:-

convertObjectToCollection: function(rawObject){
  var hashMapObject = Ext.create('Ext.util.HashMap'); 
    for(var key in rawObject) {
      hashMapObject.add(key,rawObject[key]);
    } 
  return hashMapObject;
}

Thanks!

If brevity is all you are after you don't really need a seperate utility - at least on a small scale:

var obj = { foo: 1, bar: 2, baz: 3 },
    map = Ext.create('Ext.util.HashMap');
Ext.iterate(obj, map.add, map);

There's no built in method. You could add to HashMap to make it more clean:

Ext.require('Ext.util.HashMap', function() {
    Ext.util.HashMap.fromObject = function(o) {
        var map = new Ext.util.HashMap(),
            key;

        for (key in o) {
            map.add(key, o[key]);
        } 
        return map;
    }
});

Ext.util.HashMap.fromObject({foo: 1, bar: 2});

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