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,411
  • 2
  • 6
  • 18
Ninja
  • 404
  • 3
  • 9
  • 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,185
  • 9
  • 10
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,411
  • 2
  • 6
  • 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
  • 47,282
  • 14
  • 68
  • 196