const first = '125';
const startWithFirst = arr.filter(n => first.includes(`${n}`[0]));
console.log(startWithFirst);
const first = [ 1, 2, 5 ];
const startWithFirst = arr.filter(n => first.includes(n / (10 ** (Math.log10(n) | 0)) | 0));
const first = /^[125]/;
const startWithFirst = arr.filter(n => first.test(n));
const getSrc = img => img.getAttribute('src');
// или
const getSrc = img => img.attributes.src.value;
const relativeOnly = f => img => {
const src = getSrc(img);
if (!/^https?:\/\//.test(src)) {
f(img, src);
}
};
document.querySelectorAll('img').forEach(relativeOnly((img, src) =>
img.outerHTML = `
<picture>
<source srcset="${src}" type="image/svg+xml">
${img.outerHTML}
</picture>`
));
const wrapImages = relativeOnly((img, src) => {
const picture = document.createElement('picture');
const source = document.createElement('source');
source.srcset = src;
source.type = 'image/svg+xml';
img.replaceWith(picture);
picture.append(source, img);
});
for (const n of document.getElementsByTagName('img')) {
wrapImages(n);
}
// или
Array.prototype.forEach.call(document.images, wrapImages);
function makeRandomizer([ min, max ]) {
const numbers = [...Array(max - min + 1).keys()];
return () => numbers.length
? min + numbers.splice(Math.random() * numbers.length | 0, 1)[0]
: null;
}
function makeRandomizer([ min, max ]) {
const numbers = Array.from({ length: max - min + 1 }, (n, i) => min + i);
for (let i = numbers.length; --i > 0;) {
const j = Math.random() * (i + 1) | 0;
[ numbers[i], numbers[j] ] = [ numbers[j], numbers[i] ];
}
return () => numbers.pop() ?? null;
}
const arr = Object
.entries(obj)
.sort((a, b) => b[1] - a[1])
.map(n => n[0]);
const arr = Object
.values(Object.entries(obj).reduce((acc, n) => ((acc[n[1]] ??= []).push(n[0]), acc), {}))
.reverse()
.flat();
package.json
, или .eslintrc.json
(все варианты тут). Так же и с prettier.