const data = [
{"prop":{"color":{"value":10},"diametr":{"value":15},"size":{"value":2}},"name":"результат 1"},
{"prop":{"color":{"value":15},"diametr":{"value":17},"size":{"value":8}},"name":"результат 2"},
{"prop":{"color":{"value":19},"diametr":{"value":17},"size":{"value":8}},"name":"результат 3"}
]
const search = filter =>
Object.entries(filter).reduce((agg, [key, value]) =>
agg.filter(v => v.prop[key].value === value)
, data)
console.log(search({ size: 8 }).map(v => v.name)) // ["результат 2", "результат 3"]
console.log(search({ size: 8, color: 15 }).map(v => v.name)) // ["результат 2"]
let Permissions = {canView: "True", canEdit: "False", canPrint: "True"};
function Count() {
let count = 0;
for (let elem of Object.values(Permissions)) {
if (elem === "True") count++;
}
alert(count);
}
const Permissions = {canView: "True", canEdit: "False", canPrint: "True"};
const count = Object.values(Permissions).filter(v => v == "True").length
alert(count)
const checkIntersection = (obj1, obj2) =>
(
(obj1.x >= obj2.x && obj1.x <= obj2.x + obj2.w) // левая сторона первого объекта в границах второго
|| // или
(obj2.x >= obj1.x && obj2.x <= obj1.x + obj1.w) // левая сторона второго объекта в границах первого
)
&&
(
(obj1.y >= obj2.y && obj1.y <= obj2.y + obj2.h) // верхняя планка первого объекта в границах второго
|| // или
(obj2.y >= obj1.y && obj2.y <= obj1.y + obj1.h) // верхняя планка второго объекта в границах первого
)
todo.completed = !todo.completed
if (todo.id === id) return { ...todo, completed: !todo.completed }
return todo
onChange={() => props.handleChange(props.item.id, !props.item.completed)}
handleChange(id, newCompleted) {
this.setState(prevState => {
const updatedTodos = prevState.todos.map(todo => {
if (todo.id === id) {
todo.completed = newCompleted
}
return todo
})
return {
todos: updatedTodos
}
})
const mapIds = {}
const filtered = array.filter(elem => {
const existingElement = mapIds[elem.id]
if (!existingElement)
mapIds[elem.id] = elem
else
existingElement.array1.push(...elem.array1)
return !existingElement
})
const mapIds = {}
const filtered = array.filter(elem =>
(!mapIds[elem.id] && (mapIds[elem.id] = elem)) || !mapIds[elem.id].array1.push(...elem.array1)
)
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,
}]
}, {...}]
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')
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
}