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);
v-model="selectField[index]" заменяете на v-model="item.value", например, а selectField.includes(name) на conditionList.some(n => n.value === name).
бибилиотека react-yandex-maps не имеет такой функциональности
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];
return { actionList };
$('.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);
});
Вопрос - правильный ли такой подход?
.text был успешно стилизован.
$('.form-group').trigger('input').
$pow = ceil(count($array) / 2);
$arr = array_map(function($n) use($sum, &$pow) {
$pow -= 1;
return $sum * (1 + (1 - (2 ** -abs($pow))) * ($pow < 0 ? -1 : 1));
}, $array);