const littleN = (arr, N) => arr.reduce(
(acc, cur) => {
const idx = acc.findIndex((el) => el.distance > cur.distance);
if (idx !== -1) {
acc.pop();
acc.splice(idx, 0, cur);
}
return acc;
},
Array(N).fill(arr[0])
);
If escape strings are not already part of your pattern you can add them using String.replace:
function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string }
const div = document.createElement('div');
[
['text', 'name', 'Введите имя'],
['text', 'phone', 'Введите номер телефона'],
['email', 'email', 'Введите адрес электронной почты'],
].forEach((d) => {
const input = document.createElement('input');
[input.type, input.name, input.placeholder] = d;
div.appendChild(e);
}
// ...
document.body.appendChild(div);
document.querySelector('#abc').innerText
// Uncaught TypeError: document.querySelector(...) is null
const el = document.querySelector('#abc');
const text = el === null ? 'default Text' : el.innerText;
const text = document.querySelector('#abc')?.innerText ?? 'default Text';