const grouped = Object.entries(arr.reduce((acc, n) => (
(acc[n.id] ??= []).push(n.name),
acc
), {}));
document.body.insertAdjacentHTML('beforeend', grouped
.map(([ k, v ]) => `
<ul id="list-${k}">${v.map(n => `
<li>${n}</li>`).join('')}
</ul>`)
.join('')
);
document.body.append(...grouped.map(([ id, names ]) => {
const ul = document.createElement('ul');
ul.id = `list-${id}`;
ul.append(...names.map(n => {
const li = document.createElement('li');
li.textContent = n;
return li;
}));
return ul;
}));
str.replace(/\d{3}-\d{2}/, '***-**')
// или
str.slice(0, 9) + '***-**' + str.slice(15)
// или
str.replace(/\d(?=\d*-)/g, '*')
// или
str.replace(/\d+(?=-)/g, m => '*'.repeat(m.length))
// или
str.replace(/(?<=\) ).{6}/, '***-**')
// или
str.match(/^.{9}|.{3}$/g).join('***-**')
if (sortArray[i]) {
0
? Тогда этот элемент обработан не будет - не попадёт в результирующий массив. О чём, кстати, и говорится в сообщении об ошибке, в возвращаемом вами отсортированном массиве элементов меньше, чем в исходном:expected [ Array(14980) ] to deeply equal [ Array(15000) ]
i
всегда существует, так что засовывать в firstArray
его следует без проверок.function pendulum(arr) {
arr.sort((a, b) => a - b);
const head = [];
const tail = [];
for (let i = 0; i < arr.length; i += 2) {
head.push(arr[i]);
if (i + 1 < arr.length) { // или if (arr.hasOwnProperty(i + 1)) {
tail.push(arr[i + 1]);
}
}
return [ ...head.reverse(), ...tail ];
}
const pendulum = arr => arr
.sort((a, b) => a - b)
.reduce((acc, n, i) => (acc[i & 1].push(n), acc), [ [], [] ])
.flatMap((n, i) => i ? n : n.reverse());
href="javascript:deleteRow(this);"
onclick="deleteRow(this)"
.document.querySelector('table').addEventListener('click', e => {
e.target.closest('.btn')?.closest('tr').remove();
});
нашел другой вариант
$('body').on('click', 'btn btn-warning', function() { $(this).parents('tr').remove(); });
НО проблема в том, что при нажатии удаляется полностью все строки, которые...
href
у ссылки пустой и не отменяете действие по умолчанию при клике. Надо так:$('table').on('click', '.btn.btn-warning', function(e) {
e.preventDefault();
$(this).closest('tr').remove();
});
#
ссылкам в href
. Или, замените a
на span
, и стилизуйте его под ссылку:span.btn {
text-decoration: underline;
color: blue;
cursor: pointer;
}
sendRequesr
вместо строки с городом соответствующий ей элемент <a>
. {"value":"[\"2b2df5f4-256d-402e-b074-c460ee394a2e
[
видите? - value (days тоже) это строка, а не массив. Так что не хватает JSON.parse
. tbody
у таблицы? Одна штука:document.querySelector('tbody').addEventListener('input', function() {
const data = Array.from(
this.children,
tr => Array.from(tr.querySelectorAll('input'), input => input.value)
);
console.log(data);
});
document.querySelector('table').addEventListener('input', e => {
const { map, flatMap } = Array.prototype;
const data = flatMap.call(
e.currentTarget.tBodies,
tbody => map.call(
tbody.rows,
tr => map.call(tr.cells, td => td.lastElementChild.value)
)
);
console.log(data);
});
// или
document.querySelector('table').addEventListener('input', function() {
const numHeadRows = this.querySelectorAll('thead tr').length;
const data = [];
for (const input of this.querySelectorAll('tbody input')) {
const td = input.parentNode;
const iCol = td.cellIndex;
const iRow = td.parentNode.rowIndex - numHeadRows;
(data[iRow] ??= [])[iCol] = input.value;
}
console.log(data);
});
const parent = document.querySelector('.top');
const elements = parent.children;
parent.replaceChildren(...Array.prototype.filter.call(
elements,
function({ textContent: n }) {
return !this.has(n) && this.add(n);
},
new Set
));
const { size: numUnique } = Array.prototype.reduce.call(
elements,
(acc, n) => acc.add(elements[acc.size].innerText = n.innerText),
new Set
);
for (; elements[numUnique]; parent.lastElementChild.remove()) ;
parent.innerHTML = Array
.from(new Set(Array.from(elements, n => n.outerHTML)))
.join('');
Object
.values(Array
.from(elements)
.reduce((acc, n) => ((acc[n.innerText] ??= []).push(n), acc), {}))
.forEach(n => n.forEach((m, i) => i && parent.removeChild(m)));
[...elements]
.filter((n, i, a) => n !== a.find(m => m.innerText === n.innerText))
.forEach(n => n.replaceWith());
class DoublyLinkedList {
constructor() {
this.size = 0;
this.head = null;
this.tail = null;
}
add(value, index) {
index ??= this.size;
const next = this.searchByIndex(index);
const prev = next ? next.prev : this.tail;
const node = { value, next, prev };
prev || (this.head = node);
next || (this.tail = node);
prev && (prev.next = node);
next && (next.prev = node);
this.size++;
}
_remove(node) {
if (node) {
node.prev || (this.head = node.next);
node.next || (this.tail = node.prev);
node.prev && (node.prev.next = node.next);
node.next && (node.next.prev = node.prev);
this.size--;
}
}
removeByValue(value) {
this._remove(this.searchByValue(value));
}
removeByIndex(index) {
this._remove(this.searchByIndex(index, true));
}
searchByIndex(index, strict) {
if (!(index >= 0 && index <= this.size - !!strict)) {
throw 'invalid index';
}
let node = this.head;
while (index--) {
node = node.next;
}
return node;
}
searchByValue(value, startIndex = 0) {
let node = this.searchByIndex(startIndex, true);
while (node && node.value !== value) {
node = node.next;
}
return node;
}
}
const input = document.querySelector('input');
const itemClass = 'word';
const blockClass = 'block';
input.addEventListener('input', e => {
const val = +e.target.value;
document.querySelectorAll(`.${itemClass}`).forEach(n => {
n.closest(`.${blockClass}`).hidden = +n.innerText < val;
});
});
.hidden {
display: none;
}
input.oninput = function() {
const val = Number(this.value);
for (const n of document.getElementsByClassName(itemClass)) {
let block = n;
while (!(block = block.parentNode).classList.contains(blockClass)) ;
block.classList.toggle('hidden', Number(n.textContent) < val);
}
};
function max(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let result = null;
for (const n of data) {
const val = getVal(n);
result = (!result || result[1] < val) ? [ n, val ] : result;
}
return result?.[0];
}
const { text } = max(arr, n => n.text.length);
const oldest = max(arr, 'age');
const tableSelector = 'здесь селектор вашей таблицы';
const columnIndices = [ 8, 9, 10 ];
const $rows = $(`${tableSelector} tr`);
// или
const { rows } = document.querySelector(tableSelector);
$rows.each((_, n) =>
$('> *', n)
.filter(i => ~columnIndices.indexOf(i))
.remove()
);
$rows
.children(`${columnIndices.map(n => `:nth-child(${-~n})`)}`)
.detach();
Array.prototype.forEach.call(rows, function(n) {
for (let i = n.cells.length; i--; this.has(i) && n.deleteCell(i)) ;
}, new Set(columnIndices));
for (const { cells } of rows) {
columnIndices.map(n => cells[n]).forEach(n => n?.remove());
}
for (const n of rows) {
n.replaceChildren(...Array.prototype.filter.call(
n.cells,
(_, i) => !columnIndices.includes(i)
));
}
function getWeekdaysOfMonth(year, month) {
const date = new Date(year, --month, 1);
const result = [];
while (date.getMonth() === month) {
result.push(date.toLocaleString('ru-RU', {
month: 'long',
day: 'numeric',
weekday: 'long',
}));
date.setDate(date.getDate() + 1);
}
return result;
}
const weekdaysOfDecember2020 = getWeekdaysOfMonth(2020, 12);
но как поступить если я не хочу забирать дни недели из стандартного объекта. а взять из их своего массива?
const weekdays = [
'воскресенье',
'это понедельник',
'а это вторник',
'конечно же среда',
'четверг',
'пятница - прямо после четверга',
'суббота, рабочая неделя окончена',
];
const getWeekdaysOfMonth = (year, month) => Array.from(
{ length: new Date(year, month--, 0).getDate() },
(n, i) => {
const d = new Date(year, month, i + 1);
return d.toLocaleString('ru-RU', {
month: 'long',
day: 'numeric',
}) + ', ' + weekdays[d.getDay()];
});
const weekdaysOfFebruary2021 = getWeekdaysOfMonth(2021, 2);
document.querySelector('.products__body').addEventListener('click', e => {
const item = e.target.closest('.card-preview__item');
if (item) {
e.preventDefault();
const { srcset } = item.querySelector('source');
item.closest('.card').querySelector('.card-head__image source').srcset = srcset;
}
});
For DOM trees which represent HTML documents, the returned tag name is always in the canonical upper-case form.
if(str.tagName == 'ul') {
} else if (str.tagName == 'li') {
str
, что за странный выбор имени? Там же элемент, а не строка.elem.append('li');
for (let el of strLi) { el.addEventListener('click',func); };
func
вынесено за пределы текущей функции, иначе бы при каждом клике всем существующим li
добавлялся новый обработчик.li
, на свежесозданных li
клик обрабатываться не будет (касается и тех, что изначально существуют).li
- так зачем назначать отдельный обработчик клика? То, что делаете в func
, вполне можно делать прямо тут.document.querySelector('ul').addEventListener('click', e => {
const t = e.target;
const ct = e.currentTarget;
t.insertAdjacentHTML('beforeend', ct === t ? '<li>text</li>' : '!');
});