.row
.row_3
.row_5
document.querySelectorAll('.row').forEach(addClass);
// или
for (const n of document.getElementsByClassName('row')) {
addClass(n);
}
const addClass = el => el.classList.add(`row-${el.children.length}`);
const classes = {
3: 'row-3',
5: 'row-5',
7: 'row-xxx',
};
function addClass(el) {
const className = classes[el.children.length];
if (className) {
el.classList.add(className);
}
}
const classes = [
{ max: 3, name: 'row-0-3' },
{ max: 5, name: '' }, // если дочерних элементов 4 или 5, класс не добавляется
{ max: 7, name: 'row-6-7' },
{ max: Infinity, name: 'row-7-x' },
];
function addClass(el) {
const { name } = classes.find(n => n.max >= el.children.length) ?? {};
if (name) {
el.classList.add(name);
}
}
const classes = [
[ c => c < 5, 'row-0-4' ],
[ c => c % 2, 'row-odd' ],
[ c => 6 < c && c < 9, 'row-7-8' ],
];
const addClass = ({ classList, children: { length } }) =>
classList.add(...classes.reduce((acc, n) => (
n[0](length) && acc.push(n[1]),
acc
), []));