This is more like a silly question but I have to ask it. It is a good practice to call an array function like map without arguments? Let's say I already have an array 'x' on my code with a specified length and I want to make another array with the same length but with different content. For example:
function generateRandomContent() { ... }
const x = [1, 2, 3, 4, 5]
const y = x.map(() => generateRandomContent())
// or
const z = Array(x.length).fill().map(() => { ... })
Of course I could pass an argument and never use it, but it is there a good way and a bad way to do it?
.map()is not supposed to be used without arguments. You could use.forEach()instead or a for loop.Array.from({length: x.length}, () => someFn())or evenArray.from({length: x.length}, someFn)will generate an array as the same size with different contents..mapsignifies that the content of the new array is closely related to the content of the original array.Array.from, which takes a function as its second argument. E.g.:Array.from(Array(10), generateRandomContent).forEachmakes even less sense there.