1

list 1 ["ART - Run and Support","ART - Run and Support","Clt group","Clt group"] list 2 ["ART - Run and Support","Clt group",]

I want to take the 1st value in list2 and count the occurrences in list 1. "ART - Run and Support" = 2 I was able to take for 1 value, not sure how to pass this in loop. Please do help.

* def condition = function(x){ return x == "Application Development" }
* def output = karate.filter(Total_List_AssignmentGroup, condition).length
* print output

I was able to take for 1 value, not sure how to pass this in loop. Please do help.

1 Answers1

1

This was a fun problem ! Here is the solution, and believe it or not it is just one line:

* def list1 = ["ART - Run and Support", "ART - Run and Support", "Clt group", "Clt group"]
* def list2 = ["ART - Run and Support", "Clt group"]
* def results = list2.map(x => ({ name: x, count: list1.filter(y => x === y).length }))
* print results

Which gives you:

[
  {
    "name": "ART - Run and Support",
    "count": 2
  },
  {
    "name": "Clt group",
    "count": 2
  }
]

This is just using JavaScript array operations such as map() and filter()

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hi Peter, Thank you for the response. I tried this, I get error at x=>, Can you help in this. – user20485356 Nov 16 '22 at 11:36
  • @user20485356 I cut and pasted those exact 4 lines and it works perfectly. maybe you are on an old version of karate. I recommend you upgrade. or you can try using "old style" syntax, refer this for how: https://stackoverflow.com/a/73407959/143475 if still stuck, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Nov 16 '22 at 11:46