And we can move the visible and naming parts to a separate function:
function firstVisible(selector) {
return $(selector).filter(":visible:first");
}
function setFieldName(field, name) {
$(field).attr("name", name);
}
fieldsInfo.forEach(function (fieldInfo) {
var field = firstVisible(fieldInfo.selector);
setFieldName(field, fieldInfo.name);
});
There are other optimizations that can be done, but some of the best progress is first achieved by separating that which changes from that which doesn’t.