I have an object items = {a:"arr", b:"brr", c: "3"}.
I want to turn it into an array, but multiplied by c.
So if items = {a:"arr", b:"brr", c: "3"} i want to get:
["arr brr","arr brr","arr brr"].
You can use Array.from() to generate an array with the length of the value of c:
const items = {a:"arr", b:"brr", c: "3" }
const result = Array.from(
{ length: +items.c },
() => `${items.a} ${items.b}`
)
console.log(result)
Use Array.flatMap() with Array.from() to handle an array of objects:
const items = [{a:"arr", b:"brr", c: "3" },{a:"ann", b:"bnn", c: "1" }]
const result = items.flatMap(obj => Array.from(
{ length: +obj.c },
() => `${obj.a} ${obj.b}`
))
console.log(result)