简体   繁体   中英

How to use maps in Javascript in Karate Framework?

I am trying to use maps inside a Javascript function, to store a JSON object so that I can add any custom key and value to that object per desire.

Basically I tried this piece of code to check map functionality

 * def sample =
   """
   function(){
    var map = new Map({foo: 'bar'});
    return map;
    }
   """

  * def res =  sample()
  * print res

But it is throwing me this error

javascript evaluation failed: sample(), ReferenceError: "Map" is not defined in at line number 2

My goal is to add some keys dynamically in my predefined JSON object. karate.merge doesn't allow dynamic key as well.

Any help will be deeply appreciated. Thanks!

Karate Version: 0.9.5

In Karate you should forget about Java. JSON is a Map.

* def sample =
"""
function(){
  var map = { foo: 'bar' };
  map.someKey = 'value';
  var someDynamicKey = 'baz';
  map[someDynamicKey] = 'ban';
  return map;
}
"""

Maybe you should take some time to read the docs and examples .

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