1
* def alphabets = ["a","b","c"]
* def number = ["1","2","3"]

So that the final mapped result should be

final =[{"a":"1"},{"b":2""},{"c":"3"}]
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
Ninja
  • 433
  • 3
  • 10
  • 6
    Does this answer your question? [Creating a JavaScript Object from two arrays](https://stackoverflow.com/questions/39127989/creating-a-javascript-object-from-two-arrays) – Sivakumar Tadisetti Jul 30 '20 at 05:14

3 Answers3

3

Try this Array reduce function. It should give the expected result.

alphabets.reduce((mem, alphabet, index) => {
  mem.push({[alphabet]: number[index]});
  return mem;
}, []);
Vasanth Gopal
  • 1,215
  • 10
  • 11
3

var alphabets = ["a","b","c"]
var number = ["1","2","3"]
res=alphabets.map((e,i)=>({[e]:number[i]}))
console.log(res)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
2

Here you go. Next time maybe you shouldn't tag your question as JS / JSON ;)

* def fun = function(x, i){ var pair = {}; pair[x] = number[i]; return pair }
* def pairs = karate.map(alphabets, fun)
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248