children
даже если пустой:function unFlatten(array) {
const childrenMap = Object.create(null);
for(const item of array) {
if(item.parent in childrenMap)
childrenMap[item.parent].push(item);
else
childrenMap[item.parent] = [item];
if(!childrenMap[item.id]) childrenMap[item.id] = [];
item.children = childrenMap[item.id];
};
return childrenMap[null]
}
children
только там где нужны.function unFlatten(array) {
const childrenMap = Object.create(null);
const secondPass = [];
for(const item of array) {
if(item.parent in childrenMap)
childrenMap[item.parent].push(item);
else
childrenMap[item.parent] = [item];
if(item.id in childrenMap)
item.children = childrenMap[item.id];
else
secondPass.push(item)
};
for(const item of secondPass) {
if(item.id in childrenMap)
item.children = childrenMap[item.id];
};
return childrenMap[null]
}
function unFlatten(array) {
const map = Object.create(null);
const secondPass = [];
for(const item of array) {
map[item.id] = item;
if(item.parent in map) {
if('children' in map[item.parent])
map[item.parent].children.push(item)
else
map[item.parent].children = [item];
} else {
secondPass.push(item);
}
};
for(const item of secondPass) {
if(item.parent in map) {
if('children' in map[item.parent])
map[item.parent].children.push(item)
else
map[item.parent].children = [item];
}
};
return secondPass
}
undefined
, вместо которого с помощью nullish coalescing подставляем просто цену. А чтобы не копипастить извлечение цены, вынесем его в отдельную функцию, которую, как и сам сортируемый массив, можно сделать параметром функции сортировки.const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const sortedArr = sorted(arr, n => -(n.price.new ?? n.price).replace(/\D/g, ''));
function onContextMenu(e) {
e.preventDefault();
elementOnClick = e.target;
if(elementOnClick.tagName.toLowerCase() === 'div') {
if(elementOnClick.classList.contains('folder')) {
elementOnClick.setAttribute('contenteditable', 'true');
var range = document.createRange();
var sel = window.getSelection();
const pos = elementOnClick.childNodes[0].textContent.length
range.setStart(elementOnClick.childNodes[0], pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
elementOnClick.focus();
console.log('Elem FOLDER');
} else if(elementOnClick.classList.contains('file')) {
elementOnClick.setAttribute('contenteditable', 'true');
var range = document.createRange();
var sel = window.getSelection();
const pos = elementOnClick.childNodes[0].textContent.lastIndexOf('.')
range.setStart(elementOnClick.childNodes[0], pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
elementOnClick.focus();
console.log('Elem FILE');
}
} else {
console.log('EMPTY');
}
document.addEventListener('click', onMouseClick, false);
}
document.getElementsByClassName("my-class1")[0].className="my-class2"
document.getElementsByClassName("my-class1")[0].style="font-family: 'PT Sans';font-style: normal;"
function checkMyFirstBlock(el){
if(el.className.indexOf("my-class1-2") == -1) {
// не найдено "my-class1-2"
document.getElementsByClassName("my-class2")[0].style = "background-color:red";
} else {
// найдено
document.getElementsByClassName("my-class2")[0].style = "background-color:white";
}
}