arr.sort((a, b) => {
return Number.isNaN(+a) || Number.isNaN(+b)
? a.localeCompare(b)
: a - b;
});
// или
arr.sort((a, b) => (a - b) || a.localeCompare(b));
const sorted = (arr, keys) => arr
.map(n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
a.find((n, i) => diff = i && ((n < b[i]) ? -1 : +(n > b[i])));
return diff;
})
.map(n => n[0]);
const sortedArr = sorted(arr, n => [ +n, n.toLowerCase() ]);
chunk_size = 3
newString = ' '.join(string[i:i + chunk_size] for i in range(0, len(string), chunk_size))
data: () => ({
sortOptions: [
{ value: 'PriceMinMax', text: 'сначала дешевле' },
{ value: 'PriceMaxMin', text: 'сначала дороже' },
],
...
}),
<ul>
<li
v-for="n in sortOptions"
v-text="n.text"
:class="{ active: sortBy === n.value }"
@click="sortBy = n.value"
></li>
</ul>
li.active {
color: red;
}
data() {
return {
dates: [ массив допустимых дат, в числовом виде (количество миллисекунд) ],
disabledFn: {
customPredictor: date => !this.dates.includes(new Date(date).setHours(0, 0, 0, 0)),
},
};
},
<datepicker :disabledDates="disabledFn" />
const list = arrToSinglyLinkedList(arr, 'child');
const arrToSinglyLinkedList = (arr, nextKey = 'next') =>
arr.reduceRight((acc, n) => ({ ...n, [nextKey]: acc }), null);
// или
function arrToSinglyLinkedList(arr, nextKey = 'next') {
let list = null;
for (let i = arr.length; i--;) {
const node = Object.assign({}, arr[i]);
node[nextKey] = list;
list = node;
}
return list;
}
const arrToSinglyLinkedList = (arr, nextKey = 'next', i = 0) =>
i < arr.length
? Object.fromEntries([
...Object.entries(arr[i]),
[ nextKey, arrToSinglyLinkedList(arr, nextKey, -~i) ]
])
: null;
// или
function arrToSinglyLinkedList(arr, nextKey = 'next', i = 0, n = arr[i]) {
const node = n ? {} : null;
if (node) {
for (const k in n) {
if (n.hasOwnProperty(k)) {
node[k] = n[k];
}
}
node[nextKey] = arrToSinglyLinkedList(arr, nextKey, i + 1);
}
return node;
}
присваивание всем переменным, которые используются в компоненте, их дефолтное значение мне тоже кажется не лучшей идеей
$t = &$arr;
foreach ($keys as $key) {
if (!isset($t[$key]) || !is_array($t[$key])) {
$t[$key] = [];
}
$t = &$t[$key];
}
$t['value'] = $val;
this
- это и будет ссылка на нужный элемент. Конечно, только в том случае, если какое-то другое значение не было заранее привязано к обработчику (как такое может быть - bind и стрелочные функции).target
с помощью closest. 1 + количество_строк - количество_элементов_в_данных_текущего_столбца
чем это может быть вызвано?
useEffect(() => setIsLoadError(false), [ src ]);
<Image key={card ? card.link : 'hello, world!!'}
const parent = document.querySelector('.xlist');
const className = 'active';
const el = parent.querySelector(`:scope > .${className}`);
const index = el ? [...parent.children].indexOf(el) : -1;
let index = -1;
for (let n = el; n; n = n.previousElementSibling, index++) ;
const index = Array.prototype.findIndex.call(
parent.children,
n => n.classList.contains(className)
);
const { children } = parent;
let index = children.length;
while (index-- && !children[index].matches(`.${className}`)) ;