Посмотрел поддержку forEach: она отличная.
forEach
- принадлежащий Array.prototype
. У вас ошибка возникает из-за отсутствия NodeList.prototype.forEach
.forEach
массива:Array.prototype.forEach.call(items, function(n) {
// ...
});
for (var i = 0; i < items.length; i++) {
// ...
}
for (var n of items) {
// ...
}
const el = currentTarget.querySelector('селектор элемента');
if (el) {
el.remove();
}
const tableEl = document.querySelector('селектор_таблицы');
const colIndex = индекс_столбца;
const colData = Array.from(
tableEl.rows,
({ cells: { [colIndex]: n } }) => n && n.textContent
);
// или
const colData = Array.prototype.map.call(
tableEl.querySelectorAll(`tr > :nth-child(${colIndex + 1})`),
n => n.innerText
);
// или
const colData = [];
for (const n of table.querySelectorAll('tr')) {
colData.push((n.children[colIndex] || {}).innerHTML);
}
const parentSelector = '.parent';
const className = 'custom';
document.querySelectorAll(`${parentSelector} > .${className}`).forEach(n => {
n.classList.remove(className);
});
// или
for (const n of document.querySelector(parentSelector).children) {
n.className = n.className.replace(RegExp(`(^| )${className}(?= |$)`), '').trim();
}
.colors
вызывайте функцию пересчёта:$('.colors').on('click', '.color', function() {
$(this).toggleClass('checked');
calculate();
});
.colors .checked
и суммирование их data-price
:total += $('.colors .checked', this)
.get()
.reduce((acc, n) => acc + +n.dataset.price, 0);
formData = [];
updateFormData(newLen) {
const oldLen = this.formData.length;
if (oldLen > newLen) {
this.formData.splice(newLen, oldLen);
} else if (oldLen < newLen) {
this.formData.push(...Array.from({ length: newLen - oldLen }, n => ({
/* здесь дефолтные данные блока */
})));
}
}
<input
type="number"
[ngModel]="formData.length"
(ngModelChange)="updateFormData($event)"
>
<div *ngFor="let item of formData;">
<!-- здесь создаёте input'ы, цепляете к ним через [(ngModel)] свойства item'а -->
</div>
const values = Array.from(
document.querySelectorAll('.aside input[type="checkbox"]:checked'),
n => n.value
);
Почему console.log('\n' == separator.value);
возвращает false?
\n
- это перенос строки. Экранируйте слэш - '\\n' == separator.value
. const steps = Object.entries(answers).reduce((acc, [ key, val ]) => {
const question = questions.find(n => n.key === key);
const step = acc[val.step] = acc[val.step] || [];
step.push({
step: val.step,
question: { ...question.question },
answer: question.answer.values instanceof Object
? question.answer.values[val.answer].en
: val.answer,
});
return acc;
}, {});
const arr = JSON.parse(localStorage.getItem('arr')) || [];
arr.push(1, 2, 3);
localStorage.setItem('arr', JSON.stringify(arr));
const lowered = str.toLowerCase();
const filtered = data.filter(n => !n.name.toLowerCase().indexOf(lowered));
const filtered = data.filter(function(n) {
return n.name.toLowerCase().startsWith(this);
}, str.toLowerCase());
const filtered = data.filter(((reg, n) => reg.test(n.name)).bind(null, RegExp(`^${str}`, 'i')));