0

I have bellow this two arrays:

var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]

and I want to group their values in another array like this

var t3 = ["18:00 - 44:00", "19:00 - 23:00", "20:00 - 21:00"]

I tried some things with nested for loops and maps but I didn't even get close to what I expected.

2
  • var t3 = t1.map((x, i) => x + ' - ' + t2[i]); (using map) Commented Jul 29, 2020 at 21:43
  • A single for loop t3[i]=t1[i]+" - "+t2[i] should do it. Commented Jul 29, 2020 at 21:43

5 Answers 5

2

You could do it like this:

var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]
var t3 = t1.map((_, i) => `${t1[i]} - ${t2[i]}`);
console.log(t3);

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

Comments

1
var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]
var t3 = []

for(var i = 0; i < t1.length; i++){
    t3.push(`${t1[i]} - ${t2[i]}`);
}

Comments

0
var t3 = t1.map((t, index) => `${t} - ${t2[index]}`);

Comments

0

var t1 = ["18:00", "19:00", "20:00"];
var t2 = ["44:00", "23:00", "21:00"];
let t3 = [];

for (let i=0; i<t1.length; i++) {
    t3.push(t1[i]+" - "+t2[i]);
}

console.log(t3);

Comments

0

You can map through the first array and adding the value of the second array, by using the index value :

var t1 = ["18:00", "19:00", "20:00"]
var t2 = ["44:00", "23:00", "21:00"]

var t3 = t1.map((element, index) => {
  return element + ' - ' + t2[index]
})

The map method offers access to the index

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.