const createArr = (source, maxLength) =>
[...Array(1 + Math.random() * maxLength | 0)].map(() => source[Math.random() * source.length | 0]);
const [ arr1, arr2, arr3 ] = [...Array(3)].map(() => createArr(arr, 5));
const createArr = ([...source], maxLength) => Array.from(
{ length: Math.min(source.length, 1 + Math.random() * maxLength | 0) },
() => source.splice(Math.random() * source.length | 0, 1)[0]
);
function createArr(source, maxLength) {
const arr = source.slice();
for (let i = arr.length; --i > 0;) {
const j = Math.random() * (i + 1) | 0;
[ arr[j], arr[i] ] = [ arr[i], arr[j] ];
}
return arr.slice(0, 1 + Math.random() * maxLength | 0);
}
function createTable(rows, cols) {
const maxLen = `${rows * cols}`.length;
return [...Array(rows)]
.map((n, i) => [...Array(cols)]
.map((m, j) => `${cols * i + j + 1}`.padStart(maxLen, 0))
.join(' '))
.join('\n');
}
function createTable(rows, cols) {
const zeroStr = Array(1 + Math.ceil(Math.log10(rows * cols + 1))).join(0);
let result = '';
for (let i = 0; i < rows; i++) {
result += i ? '\n' : '';
for (let j = 0; j < cols; j++) {
result += (j ? ' ' : '') + (zeroStr + (cols * i + j + 1)).slice(-zeroStr.length);
}
}
return result;
}
const SHOW_INITIAL = 3;
const SHOW_MORE = 5;
const listSelector = '.myList';
const itemSelector = 'li';
const showSelector = '.loadMore';
const hideSelector = '.showLess';
$(listSelector)
.on('click', showSelector, function({ delegateTarget: t }) {
$(`${itemSelector}:hidden`, t).slice(0, SHOW_MORE).show();
})
.on('click', hideSelector, function({ delegateTarget: t }) {
$(`${itemSelector}:visible`, t).slice(SHOW_INITIAL).slice(-SHOW_MORE).hide();
})
.each(function() {
$(itemSelector, this).slice(0, SHOW_INITIAL).show();
});
g
. Хотите найти всё - используйте preg_match_all. const types = [ 'text', 'password', 'number' ];
const [ type, setType ] = useState('text');
<input type={type} />
const onTypeChange = e => setType(e.target.value);
{types.map(n => (
<input
type="button"
value={n}
className={n === type ? 'active' : ''}
onClick={onTypeChange}
/>
))}
{types.map(n => (
<label>
<input
type="radio"
value={n}
checked={n === type}
onChange={onTypeChange}
/>
{n}
</label>
))}
<select value={type} onChange={onTypeChange}>
{types.map(n => <option>{n}</option>)}
</select>
<button onClick={() => setType(types[(types.indexOf(type) + 1) % types.length])}>
next type
</button>
const sorted = (arr, path) => arr
.map(function(n) {
return [ n, this.reduce((p, c) => p?.[c], n) ];
}, path.split('.'))
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const sortedByCommentsCount = sorted(arr, n => n.comments.count);
const sortedByLengthDesc = sorted(arr, n => -n.length);
document.querySelector('form').addEventListener('submit', function(e) {
e.preventDefault();
const data = Object.fromEntries(new FormData(this));
console.log(JSON.stringify(data, null, 2));
});
const pickers = $('селектор элементов, на которых инициализируются экземпляры календаря')
.datepicker({
onSelect(formattedDate, date, picker) {
pickers.forEach(n => n !== picker && (
n.currentDate = picker.currentDate,
n.selectedDates = [ date ],
n.update()
));
},
})
.get()
.map(n => $(n).data('datepicker'));
const options = {
onSelect({ date, datepicker }) {
pickers.forEach(n => n !== datepicker && n.update({
viewDate: datepicker.viewDate,
selectedDates: [ date ],
}, {
silent: true,
}));
},
};
const pickers = Array.from(
document.querySelectorAll('селектор элементов с календарями'),
n => new AirDatepicker(n, options)
);
<select id="country"></select>
<select id="city"></select>
const setOptions = (el, data) =>
el.innerHTML = data
.map(n => `<option>${n}</option>`)
.join('');
const countries = [
{ name: 'Германия', cities: [ 'Берлин', 'Бонн', 'Мюнхен' ] },
{ name: 'Франция', cities: [ 'Париж', 'Лион', 'Марсель' ] },
{ name: 'Италия', cities: [ 'Рим', 'Неаполь', 'Милан' ] },
];
const country = document.querySelector('#country');
const city = document.querySelector('#city');
setOptions(country, countries.map(n => n.name));
country.addEventListener('change', function() {
setOptions(city, countries.find(n => n.name === this.value).cities);
});
country.dispatchEvent(new Event('change'));
const sortedInventory = Object
.values(inventory)
.sort((a, b) => a.price - b.price)
.map(n => `${n.title} - ${n.amount}`)
.join('\n');