简体   繁体   中英

How to merge multiple array in karate framework?

I can add multiple array using Javascript with below code but when I apply same in Karate framework, it gives me error. Any help resolving this issue.

Javascript:

var array1 = ["02","03"];
var array2 = ["24","21"];
var array3 = ["12","13"];

function mergeElementsAtIndex(array1, array2, array3) {
      var newArray = array1.map(function(value, index) 
      {
        return value + array2[index] + array3[index];
      });
        return newArray;
    }

 - List item

merged_array = mergeElementsAtIndex(array1, array2, array3)

Same in Karate:

* def mydate = 
    """
    function (array1, array2, array3) {
      var newArray = array1.map(function(value, index) 
      {
        return value + array2[index] + array3[index];
      });
        return newArray;
    }
    """
* def newdissue = mydate(array1, array2, array3)

I am getting error as :

*** step failed: testing.feature:97 - javascript evaluation failed: mydate(array1, array2, array3), TypeError: issueMonth.map is not a function in <eval> at line number 2

Use karate.append() : https://github.com/intuit/karate#json-transforms

* def temp1 = [1, 2, 3]
* def temp2 = karate.append(temp1, [4, 5], [6, 7])

Solution for above issue:

* def mydate = 
"""
function (array1,array2,array3) {
  array1=karate.append(array1);
  array2=karate.append(array2);
  array3=karate.append(array3);
  var c = [];

      for(var i=0;i<Math.max(array1.length,array2.length,array3.length); i++)
    {
        c.push((array1[i]|| 0)+(array2[i]|| 0)+(array3[i]|| 0))
    }
    return c
}
"""
* def expireDate = mydate(expireMonth,expireDay,expireYear)

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