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);

1
  • { x: id, y: b[i] } represents an object. Commented Oct 17, 2019 at 10:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.