Напишите функцию replacer для JSON-преобразования, которая удалит свойства, ссылающиеся на meetup
function makeReplacer() {
const path = [];
return (k, v) => {
// простое значение не записываем в путь
if (!v || typeof v !== 'object') {
return v;
}
// находим родительский объект; всё что глубже, выкидываем
for (let i = path.length - 1; i >= 0; --i) {
const item = path[i];
if (Object.prototype.hasOwnProperty.call(item.obj, k) && item.obj[k] === v && !item.handled.has(k)) {
item.handled.add(k);
break;
} else {
path.pop();
}
}
// если в парентах уже был текущий объект, то игнорируем его
if (path.find((item) => item.obj === v)) {
return undefined;
}
// записываем объект в путь и возвращаем
path.push({
obj: v,
handled: new Set()
});
return v;
};
}
// -----------------------------
// пример использования
let room = {
number: 23
};
let obj1 = {a: {b: room, bb: 23}, c: 34};
let meetup = {
title: "Совещание",
occupiedBy: [room, {name: "Иванов"}, {name: "Петров"}],
place: room
};
room.occupiedBy = meetup;
meetup.self = meetup;
room.self = room;
room.obj1 = obj1;
console.log(JSON.stringify(meetup, makeReplacer(), 4))
const InnerBlock = ({blocks}) => {
return blocks.filter(...).map(...)
}
const getBlocks = (statuses) => {
if (statuses.length === 1) {
const sid = statuses[0].id;
return bigData.filter((a) => a.status === sid);
} else {
return bigData;
}
}
const InnerContainer = ({statuses, ...props}) => {
const [blocks, setBlocks] = useState(() => getBlocks(statuses));
useEffect(() => {
setBlocks(getBlocks(statuses));
}, [statuses]);
function sorting(){
if(...){
if(...){....setBlocks(sort)}else{setBlocks(sort)}
}else{
if(...){....setBlocks(sort)}else{setBlocks(sort)}
}
}
return(
{sorting} // где-то внутри пару раз вызывается
<InnerBlock blocks={blocks} />
)
}
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));
}