const toTree = (arr, idAttr = 'id', parentAttr = 'parentId') => {
arr.sort((a, b) =>
(a[parentAttr] === b[idAttr] && 1) ||
(b[parentAttr] === a[idAttr] && -1) ||
0)
const tree = arr.reduce((tree, data) => {
data.children = []
tree[data[idAttr]] = data;
(tree[data[parentId]] || tree).children.push(data);
return tree
}, { children: [] }).children;
return tree
}
[{
id: 1,
comment: 'Комментарий',
children: [{
id: 2,
refTo: 1.
children: ...
}, {
id: 3.
refTo: 1,
}]
}, {...}]
export default function Method({title, description, name, isActive}) {
...
<Button
name={name}
className={style.button + (isActive ? ' active-class' : '')}
export default function Method({title, description, name, activeName}) {
const isActive = name === ActiveName
...
<Button
name={name}
className={style.button + (isActive ? ' active-class' : '')}
let key, count
const select = document.getElementById('group-id-170')
window.addEventListener('keyup', e => {
const k = e.key.toUpperCase()
if (k === key) count++
else {
key = k
count = 0
}
const highlighted = select.querySelector('.is-highlighted')
if (highlighted) {
highlighted.classList.toggle('is-highlighted')
}
const el = Array.from(select.querySelectorAll('option')).filter(el =>
el.innerText[0].toUpperCase() === key
)[count]
if (el) el.classList.toggle('is-highlighted')
})
jest.mock('axios')
wrapper.find('button').trigger('click')
axios.get.mockResolvedValue(someMockData);
jest.mock('axios')
SELECT doc1Groupped.maxId, doc1Groupped.maxVersion, doc2.id
FROM (
SELECT MAX(doc1.id) as maxId, MAX(doc1.version) as maxVersion
FROM doc1
GROUP BY doc1.date, doc1.title
) as doc1Groupped
JOIN doc2 ON doc1Groupped.maxId = doc2.id
WHERE doc2.id is NULL OR doc2.history = ''
const transform = data => {
const res = data.reduce((agg, item) => {
item = {...item}
if (agg[agg.length - 1]?.type !== item.type)
agg.push({ type: item.type, children: [] })
agg[agg.length - 1].children.push((delete item.type, item))
return agg
})
res.filter(v => !v.type.startsWith('Section')).forEach(v => {
v.type = 'Section' + v.type
})
return res
}
Вроде бы понятно, что префиксы(--i, ++i) быстрее постфиксов(i--, i++)
Когда-то и где-то читал, что лучший пробег - это с конца до начала, используя конструкцию:
for(let i = array.length - 1; i >= 0; --i)
for(let i = 0; i < array.length; ++i) let value = array[i]