-5

How do I achieve this in Javascript?

My problem is e.g. templateMonth (string/single line value); templateName (array value)

const a = 2022-01
const b = ['HD-HD1460', 'HD-WHITE', 'HD-YHS']

My desired output is

2022-01; HD-HD1460 
2022-01; HD-WHITE
2022-01; HD-YHS

Thank you.

ely27
  • 1
  • 1

2 Answers2

3

Just loop through all elements in b and simply output a in the loop. The loop has access to variables outside of it.

Your a should also be changed to a string, since 2022-01 will substract 1 from 2022, probably not what you intended.

const a = '2022-01'
const b = ['HD-HD1460', 'HD-WHITE', 'HD-YHS']

b.forEach(item=>console.log(`${a}; ${item}`))

Consider searching the internet for JavaScript documentation and tutorials and make a first effort yourself.

Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
Usama Masood
  • 141
  • 5
1

you can combine them into an array of object or if you needjust the console.log you can do as @Usama Masood suggested

const a = '2022-01'
const b = ['HD-HD1460', 'HD-WHITE', 'HD-YHS']

const combined = b.map(name => ({date: a, name}))

combined.forEach(c => console.log(`${c.date} ${c.name}`))

//or simply

b.forEach(i => console.log(`${a} ${i}`))
R4ncid
  • 6,944
  • 1
  • 4
  • 18