将两个向量拼合为一张地图的好方法?

时间:2018-09-04 10:50:10

标签: xpages xpages-ssjs

是否有更好的方法将Notes文档中的两个多值字段展平为SSJS中的地图集合?

到目前为止,我来到了以下地方:

var vw:NotesView = database.getView("configuration");
    if(null != vw){
        var doc:NotesDocument = vw.getFirstDocument();
        if (null != doc){
            var itemname1:string = "userName";
            var itemname2:string = "inboxFile";
            if(doc.hasItem(itemname1) && doc.hasItem(itemname2)){
                var itemvalues1:java.util.Vector = doc.getItemValue(itemname1);
                var itemvalues2:java.util.Vector = doc.getItemValue(itemname2);

                if(itemvalues1.size() == itemvalues2.size()){
                    var map = new java.util.HashMap();
                    var iterator1 = itemvalues1.iterator();
                    var iterator2 = itemvalues2.iterator();                 
                }               

                while (iterator1.hasNext()) {
                    var itemvalue1 = iterator1.next();
                    var itemvalue2 = iterator2.next();
                    if ((typeof(itemvalue1)).endsWith("string")) {
                        map.put(itemvalue1.left(80), itemvalue2.left(80));                      
                    }
                }
                sessionScope.put("setupInbox",map)
            }           
        }
    }   

1 个答案:

答案 0 :(得分:3)

如果支持JavaScript forEach构造,则可以使用以下方法:

     function mergeVector(keyVector, valueVector) {
        var map = new java.util.HashMap();
        keyVector.forEach(function(curKey, index) {
            if (index < valueVector.size()) {
                map.put(curKey, valueVector.get(index));
            }
        });
        return map;
     }

否则使用:

     function mergeVector(keyVector, valueVector) {
        var map = new java.util.HashMap();
        for(var index = 0; index < keyVector.size(), index++) {
            if (index < valueVector.size()) {
                map.put(keyVector.get(index), valueVector.get(index));
            }
        };
        return map;
     }

希望这会有所帮助