проблема в том, что при клике по какому-либо элементу подМЕНЮ, первый, элемент который задавался по умолчанию не сбрасывает стиль
$('ul#topnav li span a').click(function(e) {
e.stopPropagation();
...
const [ key ] = Object
.entries(data)
.reduce((max, [ k, { Number_of_Something: v } ]) => {
return max[1] > v ? max : [ k, v ];
}, [ null, -Infinity ]);const key = (Object
.entries(data)
.sort((a, b) => b[1].Number_of_Something - a[1].Number_of_Something)
.shift() || [ null ])
.shift();let key = null;
let max = -Infinity;
for (const k in data) {
if (data.hasOwnProperty(k) && data[k].Number_of_Something > max) {
key = k;
max = data[k].Number_of_Something;
}
}const max = Math.max(...Object.values(data).map(n => n.Number_of_Something));
const key = Object.keys(data).find(k => data[k].Number_of_Something === max) || null;
<div class="out"></div>
<button class="btn">click me</button>const images = [ '...', '...', /* ... */ ];
const SHOW_MORE = 2;
document.querySelector('.btn').addEventListener('click', e => {
document.querySelector('.out').insertAdjacentHTML('beforeend', images
.splice(0, SHOW_MORE)
.map(n => `<div class="outImg"><img src="${n}"></div>`)
.join('')
);
e.target.disabled = !images.length;
});
$('ur_login')$('ur_password')только начал учить AJAX и PDO
const table = document.querySelector('.table-price table');.highlight-min {
color: green;
font-weight: bold;
}
.highlight-max {
color: red;
font-weight: bold;
}table.querySelectorAll('tr').forEach(n => {
const td = Array
.from(n.querySelectorAll('td:not(.logo-table)'), m => [ m, +m.innerText.match(/\d+/) ])
.sort((a, b) => a[1] - b[1])[0][0];
td.classList.add('highlight-min');
});for (const { cells: [ , ...cells ] } of table.rows) {
const [ tds ] = cells.reduce((max, n) => {
const val = parseInt(n.textContent);
if (val > max[1]) {
max = [ [], val ];
}
if (val === max[1]) {
max[0].push(n);
}
return max;
}, [ [], -Infinity ]);
tds.forEach(n => n.classList.add('highlight-max'));
}
const className = 'класс элементов, чьи id вам надо получить';const elems = document.querySelectorAll(`.${className}`);
// или
const elems = document.getElementsByClassName(className);const getId = el => el.id;
// или
const getId = el => el.getAttribute('id');
// или
const getId = el => el.attributes.id.value;const ids = Array.from(elems, getId);
// или
const ids = Array.prototype.map.call(elems, getId);
// или
const ids = [];
for (const n of elems) {
ids.push(getId(n));
}
// или
const ids = [];
for (let i = 0; i < elems.length; i++) {
ids[i] = getId(elems[i]);
}
// или
const ids = (function get(i, n = elems.item(i)) {
return n ? [ getId(n), ...get(i + 1) ] : [];
})(0);
const result = Object.values(arr.reduce((acc, n) => (
acc[n.id] = n.checked ? n : (acc[n.id] || n),
acc
), {}));const result = arr.reduce((acc, n) => {
const i = acc.findIndex(m => m.id === n.id);
if (!~i || (!acc[i].checked && n.checked)) {
acc.push(n);
if (~i) {
acc.splice(i, 1);
}
}
return acc;
}, []);
// или
const result = Object
.values(arr.reduce((acc, n, i) => (
(!acc[n.id] || (!acc[n.id][0].checked && n.checked)) && (acc[n.id] = [ n, i ]),
acc
), {}))
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const elements = document.querySelectorAll('.buy');const getText = el => el.querySelector(':scope > i').textContent;
// или
const getText = el => el.children[0].innerText;[...elements].forEach((n, i, a) => {
n.style.display = n === a.find(m => getText(m) === getText(n))
? 'block'
: 'none';
});const grouped = Array
.from(elements, n => [ n, getText(n) ])
.reduce((acc, n) => ((acc[n[1]] = acc[n[1]] || []).push(n[0]), acc), {});
Object.values(grouped).forEach(n => n.forEach((m, i) => m.hidden = !!i));.hidden {
display: none;
}elements.forEach(function(n) {
const t = getText(n);
n.classList.toggle('hidden', this.has(t) || !this.add(t));
}, new Set);
people.filter((n, i, a) => n.country && i === a.findIndex(m => m.country === n.country))people.filter(n => n.country && n === people.find(m => m.country === n.country))people.filter(function({ country: n }) {
return n && !(this[n] = this.hasOwnProperty(n));
}, {})Object.values(people.reduce((acc, n) => (n.country && (acc[n.country] = n), acc), {}))[...people.reduce((acc, n) => n.country ? acc.set(n.country, n) : acc, new Map).values()]Array.from(
new Set(people.map(n => n.country).filter(Boolean)),
n => people.find(m => m.country === n)
)
const block = document.querySelector('.block');
const inputSelector = 'input[type="number"]';const toggleBlock = () =>
block.style.display = Array
.from(document.querySelectorAll(inputSelector))
.some(n => n.value && +n.value > +n.min)
? 'block'
: 'none';
document.addEventListener('input', e => e.target.matches(inputSelector) && toggleBlock());
toggleBlock();
// или
const inputs = document.querySelectorAll(inputSelector);
const toggleBlock = () =>
block.hidden = Array.prototype.every.call(
inputs,
n => !n.value || +n.value <= +n.min
);
inputs.forEach(n => n.addEventListener('input', toggleBlock));
toggleBlock();
if ( typeof arr[i] === 'number' ) { x*= arr[i];
arr[i + 1] окажется строкой? Сначала надо проверить всё, а уже потом вычислять (если потребуется) объединённое значение.function bullshit(arr) {
if (arr.length) {
if (arr.every(n => n === `${n}`)) {
return ''.concat(...arr);
}
if (arr.every(n => n === +n)) {
return eval(arr.join('*'));
}
}
return null;
}function bullshit(arr) {
const count = arr.reduce((acc, n) => {
const type = typeof n;
acc[type] = (acc[type] || 0) + 1;
return acc;
}, {});
switch (arr.length) {
case count.string: return arr.join('');
case count.number: return arr.reduce((acc, n) => acc * n, 1);
default: return null;
}
}
const result = Object
.entries(obj)
.sort((a, b) => a[0] - b[0])
.reduce((acc, [ k, v ], i, a) => (
i && !(-~a[~-i][0] - k) || (acc[0][acc[1] = k] = 0),
acc[0][acc[1]] += v,
acc
), [ {} ])[0];
$('.wrap').on('input', function(e) {
const valid = $(this).find('.valid').get().map(el => {
if (el === e.target) {
$(el).toggleClass('empty', !el.value);
}
return el.value;
}).every(Boolean);
$('.btn')
.toggleClass('btn--isvalid', valid)
.toggleClass('btn--novalid', !valid);
});
$('.form-group').trigger('input').