0

I have two arrays namely a and b. I want to combine first value of a and first value of b as one array and that continues. (i.e)

a = ["05:25 PM", "3:05 PM"];
b = [60, 120];

Desired Output:

 ['05:25 PM', 60],
 ['3:05 PM', 120]

I have combined two array but it's not coming as expected

a = ["05:25 PM", "3:05 PM"];
b = [60, 120];
    
const result = a.map((id, i) => ({ x: id, y: b[i] }));
console.log(result);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Chris
  • 1,236
  • 2
  • 18
  • 34

1 Answers1

2

You can use map in this way instead to zip the arrays a and b:

const a = ["05:25 PM", "3:05 PM"];
const b = [60, 120];

const c = a.map((item, index) => [item, b[index]]);
console.log(c);

The only difference between my code and yours is that mine produces arrays with two elements, one from a and one from b, instead of objects.

Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46