array2.filter(function(n) {
return this.has(n.code);
}, new Set(array1.map(n => n.value)))
array1.map(function(n) {
return this[n.value];
}, Object.fromEntries(array2.map(n => [ n.code, n ])))
/*
* если вдруг в array1 могут быть объекты, value которых отсутствует среди code в array2,
* то после map'а надо будет сделать фильтрацию, .filter(Boolean), например
*/
array2.filter(n => array1.some(m => m.value === n.code))
e.target.dataset.followerStyle
e.target
? Ну, когда вы наводите курсор на слайд с data-атрибутом. На тот слайд, который не видно за вложенной в него картинкой.target
хватать, а пытаться найти у него предка с нужным вам атрибутом. return target[property]
пусть будетconst val = target[property];
return val instanceof Function ? val.bind(target) : val;
// значения withSeat вместо раскладывания в отдельные переменные собираете в объект
const withSeat = {
adult: 1,
teenager: 1,
babe: 0,
};
const passengers = cabins.flatMap(n => Object
.entries(n)
.flatMap(([ k, v ]) => Array.from(
{ length: v },
() => ({ withSeat: withSeat[k] })
))
);
const obj = Object.fromEntries(Object
.entries(arr.reduce((acc, n) => (
Object.entries(n).forEach(([ k, v ]) => (acc[k] ??= new Set).add(v)),
acc
), {}))
.map(n => [ n[0], n[1].size === 1 ? [...n[1]][0] : null ])
);
formatDate(date) {
const today = new Date();
return [ 'getFullYear', 'getMonth', 'getDate' ].every(n => date[n]() === today[n]())
? 'Today'
: flatpickr.formatDate(date, 'j M');
},
document.addEventListener('change', ({ target: t }) => {
const fieldset = t.closest('fieldset');
if (fieldset) {
const [ main, ...items ] = fieldset.querySelectorAll('input[type="checkbox"]');
if (main === t) {
items.forEach(n => n.checked = t.checked);
} else {
main.checked = items.every(n => n.checked);
}
main.closest('legend').classList.toggle('active', main.checked);
}
});
document.querySelector('.result').append(...Array
.from(document.querySelector('.data').cloneNode(true).children, n => [ n, +n.innerText ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0])
);
document.addEventListener('mouseenter', ({ target: t }) => {
if (t.tagName === 'TD') {
for (const tr of t.closest('tbody').children) {
tr.cells[t.cellIndex]?.classList.add('_hover');
}
}
}, true);
document.addEventListener('mouseleave', e => {
if (e.target.tagName === 'TD') {
document.querySelectorAll('._hover').forEach(n => n.classList.remove('_hover'));
}
}, true);
document.addEventListener('mouseover', e => {
document.querySelectorAll('._hover').forEach(n => n.classList.remove('_hover'));
const td = e.target.closest('td');
if (td) {
for (const tr of td.closest('tbody').children) {
tr.cells[td.cellIndex]?.classList.add('_hover');
}
}
});
document.querySelectorAll('.tariff__counter').forEach(n => {
const max = n.dataset.seats;
n.querySelectorAll('.tariff__counter-max').forEach(m => m.innerText = max);
n.querySelectorAll('.amount').forEach(m => m.max = max);
});
document.addEventListener('click', ({ target: t }) => {
const change = ({
'-': 1,
'+': -1,
})[t.innerText];
if (change) {
const input = t.closest('.tariff__counter-block').querySelector('.amount');
input.value -= change;
input.dispatchEvent(new Event('input', { bubbles: true }));
}
});
document.addEventListener('input', ({ target: t }) => {
if (t.classList.contains('amount')) {
t.value = Math.max(t.min, Math.min(t.max, t.value));
}
});
const result = firstArray.map(function(n) {
return this[n.id]?.[n.secondId] ?? n;
}, secondArray.reduce((acc, n) => ((acc[n.id] ??= {})[n.secondId] = n, acc), {}));
function getLeaves(tree) {
const result = [];
const arr = [...tree];
for (const n of arr) {
if (Array.isArray(n.children) && n.children.length) {
arr.push(...n.children);
} else {
result.push(n);
}
}
return result;
}
function getLeaves(tree) {
const result = [];
const stack = [];
for (let arr = tree, i = 0; i < arr.length || stack.length; i++) {
if (i === arr.length) {
[ arr, i ] = stack.pop();
} else if (Array.isArray(arr[i].children) && arr[i].children.length) {
stack.push([ arr, i ]);
arr = arr[i].children;
i = -1;
} else {
result.push(arr[i]);
}
}
return result;
}
const groupSum = (arr, keyProp, ...sumProps) =>
Object.values(arr.reduce((acc, n) => (
acc[n[keyProp]] ??= { ...n, ...Object.fromEntries(sumProps.map(m => [ m, 0 ])) },
sumProps.forEach(m => acc[n[keyProp]][m] += n[m]),
acc
), {}));
const result = groupSum(arr, 'product', 'price', 'selling_price', 'quantity');
document.querySelectorAll('.elem button').forEach(n => {
n.addEventListener('click', onPlusMinusClick);
});
document.querySelectorAll('.btn-all').forEach(n => {
n.addEventListener('click', onGetDataClick);
});
function onPlusMinusClick({ target }) {
const input = target.closest('.elem').querySelector('input');
const change = target.innerText === '+' ? -1 : 1;
input.value = Math.max(input.min, Math.min(input.max, input.value - change));
}
function onGetDataClick({ target }) {
const data = Array
.from(target.closest('.child-block').querySelectorAll('.elem'))
.flatMap(n => Array(+n.querySelector('input').value).fill(+n.dataset.id));
console.log(data);
}
const obj = {
a: {
b: {
c: {
xxx: 666,
},
},
},
};
const proxy = new Proxy(obj, {
get: (target, key) => key.split('.').reduce((p, c) => p?.[c], target),
});
console.log(proxy['a.b.c.xxx']); // 666