const handleClickRMB = useCallback((event) => {
event.preventDefault();
let index1 = Number(event.path[0].attributes.pos1.value);
let index2 = Number(event.path[0].attributes.pos2.value);
let pressedCell = board[index1][index2];
setFlagsLeft(flagsLeft => {
if ((!pressedCell.opened) && (flagsLeft !== 0)) {
const marked = pressedCell.marked;
pressedCell.mark();
changeOneCell(index1, index2, pressedCell);
return marked ? flagsLeft + 1 : flagsLeft - 1;
}
return flagsLeft;
});
}, [setFlagsLeft, changeOneCell]);
useEffect(() => {
document.addEventListener('contextmenu', handleClickRMB, false);
return () => document.removeEventListener('contextmenu', handleClickRMB, false);
}, [handleClickRMB]);
function createMap(arr) {
const map = new Map();
arr.forEach(a => {
let childs = map.get(a.parentId);
if (!childs) {
map.set(a.parentId, childs = []);
}
childs.push(a);
});
return map;
}
function filter(map, initial) {
const result = (map.get(initial) || []).slice();
for (let i = 0; i < result.length; ++i) {
result.push(...(map.get(result[i].id) || []));
}
return result;
}
// использование
const initial = 1
const arr = [
{ name: '1', parentId: 1, id: 2 },
{ name: '2', parentId: 2, id: 3 },
{ name: '3', parentId: 1, id: 4 },
{ name: '3', parentId: 4, id: 5 }
];
var map = createMap(arr);
console.log(filter(map, initial));
console.log(filter(map, 2));
const full = '#'.repeat(7); // полная строка
for (let i = 1; i <= 7; i++){
console.log(full.substr(0, i));
}
let today = [yearToday, monthToday, dayToday].map(s => s.padStart(2, '0')).join('-')