const newStr = str.replace(/\d.*/, '');
// или
const newStr = str.match(/^\D*/)[0];
// или
const newStr = str.split(/[0-9]/).shift();
// или
const newStr = str.slice(0, str.search(/\d|$/));
// или
const newStr = /[^\d]*/.exec(str).pop();
// или
const newStr = [...str].reduceRight((acc, n) => '0123456789'.includes(n) ? '' : acc + n, '');
const sectionAttribute = element.getAttribute('data-content')
const titles = document.querySelectorAll('[data-title]');
const contents = document.querySelectorAll('[data-content]');
titles.forEach(n => n.addEventListener('click', onClick));
function onClick({ currentTarget: t }) {
titles.forEach(n => n.classList.toggle('active', n === t));
contents.forEach(n => n.hidden = n.dataset.content !== t.dataset.title);
}
const [ coord, setCoord ] = useState(...);
function onActionTickComplete(e) {
const projection = e.get('target').options.get('projection');
const { globalPixelCenter, zoom } = e.get('tick');
setCoord(projection.fromGlobalPixels(globalPixelCenter, zoom));
}
<Map
onActionTickComplete={onActionTickComplete}
...
>
<Placemark geometry={coord} />
</Map>
Object.values(arr.reduce((acc, { name, ...n }) => (
Object.values(n).pop().forEach(({ scores, channel }) =>
scores.forEach(({ score }) =>
(acc[channel]?.score > score) || (acc[channel] = { channel, name, score })
)
),
acc
), {}))
const toggleSearchParams = params => {
const newSearchParams = [...searchParams];
for (const n of params) {
const index = newSearchParams.findIndex(m => n[0] === m[0] && n[1] === m[1]);
if (index === -1) {
newSearchParams.push(n);
} else {
newSearchParams.splice(index, 1);
}
}
setSearchParams(newSearchParams);
};
const handleChangeCheckBoxValue = e => {
toggleSearchParams([ [ 'selected', e.target.value ] ]);
};
const handleDeleteParams = () => {
toggleSearchParams(checkboxParams.map(n => [ 'selected', n ]));
};
swiper.on('slideChange', ({ activeIndex }) => {
document.querySelectorAll('.points .point').forEach((n, i) => {
n.classList.toggle('active', i === activeIndex);
});
})
document.querySelector('.points').addEventListener('click', ({ target: t }) => {
if (t.classList.contains('point')) {
swiper.slideTo([...t.parentNode.children].indexOf(t));
}
});
pagination: {
el: '.points',
bulletClass: 'point',
bulletActiveClass: 'active',
renderBullet: (index, className) => `<div class="${className}">${index + 1}</div>`,
clickable: true,
},
const payload = {
data: Object.fromEntries([
'раз свойство',
'два свойство',
'три свойство',
].map(n => [ n, this[n] ])),
};
Удалили под индексом 2 <...> элемента под индексом 2 нет
как вернуть к первоначальному состоянию когда unchecked не понимаю
function Posts() {
const [ posts, setPosts ] = useState([]);
const [ filteredPosts, setFilteredPosts ] = useState([]);
const [ search, setSearch ] = useState('');
const [ checkbox, setCheckbox ] = useState(false);
useEffect(() => {
axios
.get('https://jsonplaceholder.typicode.com/posts')
.then(r => setPosts(r.data));
}, []);
useEffect(() => {
let filtered = posts;
if (search) {
const s = search.toLowerCase();
filtered = filtered.filter(n => n.title.toLowerCase().includes(s));
}
if (checkbox) {
filtered = filtered.filter(n => n.userId === 10);
}
setFilteredPosts(filtered);
}, [ posts, search, checkbox ]);
return (
<>
<input
type="text"
placeholder="Search for article..."
value={search}
onChange={e => setSearch(e.target.value)}
/>
<label>
User Id 10
<input
type="checkbox"
checked={checkbox}
onChange={e => setCheckbox(e.target.checked)}
/>
</label>
<ul>
{filteredPosts.map(n => (
<li key={n.id}>
<div>{n.userId}</div>
<div>{n.title}</div>
<div>{n.body}</div>
</li>
))}
</ul>
</>
);
}
new MutationObserver((mutations, observer) => {
if (mutations[0].target.classList.contains('интересующий-вас-класс')) {
observer.disconnect();
typed.start();
}
}).observe(элементНаличиеКлассаУКоторогоНадоОтследить, { attributes: true });
const getTheNearestLocation = (locations, [ x, y ]) =>
locations.reduce((nearest, n) => {
const d = ((n[1][0] - x) ** 2 + (n[1][1] - y) ** 2) ** 0.5;
return nearest[1] < d ? nearest : [ n, d ];
}, [ null, Infinity ])[0];
const parent = document.querySelector('ul');
const id = 'item';
const elems = Array.from(parent.children);
const index = elems.findIndex(n => n.id === id);
const result = index === -1 ? elems : elems.slice(0, index);
// или
const result = [...parent.querySelectorAll(`:not(#${id}, #${id} ~ *)`)];
// или
const result = [];
for (
let el = parent.firstElementChild;
el && el.id !== id;
el = el.nextElementSibling
) {
result.push(el);
}
{items.map(n => <div className="item">...</div>)}
.item:nth-child(6n + 3),
.item:nth-child(6n + 4) {
...
}
const makeCensored = (str, words, replacement = '***') =>
str
.split(' ')
.map(n => words.includes(n) ? replacement : n)
.join(' ');
function merge($idKey, $mergeKeys, ...$data) {
$merged = [];
foreach (array_merge(...$data) as $item) {
$id = $item[$idKey];
if (!array_key_exists($id, $merged)) {
$merged[$id] = [
'unique' => true,
'item' => $item,
];
} else {
if ($merged[$id]['unique']) {
$merged[$id]['unique'] = false;
foreach ($mergeKeys as $k) {
$merged[$id]['item'][$k] = [ $merged[$id]['item'][$k] ];
}
}
foreach ($mergeKeys as $k) {
$merged[$id]['item'][$k][] = $item[$k];
}
}
}
return array_column($merged, 'item');
}
$merged = merge('code', [ 'quantity', 'city' ], $arr1, $arr2);