Reputation: 741
I use map everywhere in my project, even when i don't want to return anything, i simply use {}
instead of ()
and don't return anything and simply use it like a for loop
, is this okay? and if it is, what about not using any key ?, because i sometimes don't really work with elements inside the map, i don't even need to (or know how) to use key for it!
Here is an example which i copied from my code:
rawElementList.map(element => {
if (element.validation !== undefined) {
if (JavascriptUtils.hasOwnProperty(element.validation, 'isRequired')) {
const isRequired = element.validation.isRequired;
if (isRequired !== undefined) {
delete element.validation.isRequired;
required.push(String(element.id))
}
}
validationJsonList.properties[element.id] = element.validation;
}
})
Upvotes: 2
Views: 1718
Reputation: 4107
No, that is not appropriate. You will greatly confuse any other readers of your code when you use a method specifically designed to create an array, but don't actually create a meaningful array. Use forEach
instead - it's the most appropriate array method for just side-effects:
rawElementList.forEach(element => {
if (element.validation !== undefined) {
if (JavascriptUtils.hasOwnProperty(element.validation, 'isRequired')) {
const isRequired = element.validation.isRequired;
if (isRequired !== undefined) {
delete element.validation.isRequired;
required.push(String(element.id))
}
}
validationJsonList.properties[element.id] = element.validation;
}
})
Upvotes: 6