const key = 'workplace';
const values = [ 'office', 'hotel' ];
const result = arr.filter(function(n) {
return this.has(n[key]);
}, new Set(values));
// или
const result = values.flatMap(((grouped, n) => grouped[n] ?? []).bind(
null,
arr.reduce((acc, n) => ((acc[n[key]] = acc[n[key]] ?? []).push(n), acc), {})
));
// или
const result = [];
for (const n of arr) {
for (const m of values) {
if (m === n[key]) {
result.push(n);
break;
}
}
}
// или
const result = [];
for (let i = 0; i < arr.length; i++) {
if (~values.indexOf(arr[i][key])) {
result[result.length] = arr[i];
}
}
// или
const result = (function get(i, n = arr[i]) {
return n
? [].concat(values.includes(n[key]) ? n : [], get(-~i))
: [];
})(0);
str.replace(/:$/, '')
// или
str.match(/[^:]+:[^:]+/)[0]
// или
str.match(/[^:]+/g).slice(0, 2).join`:`
// или
str.split(':', 2).join(':')
// или
str.slice(0, str.length - (str.slice(-1) === ':'))
function getGridSize() {
const w = window.innerWidth;
return [ 500, 700, 1225, Infinity ].findIndex(n => n > w) + 1;
}
function getGridSize() {
const w = window.innerWidth;
return [
{ size: 1, maxWidth: 500 },
{ size: 2, maxWidth: 700 },
{ size: 3, maxWidth: 1225 },
{ size: 4, maxWidth: Infinity },
].find(n => n.maxWidth > w).size;
}
const isEqual = (a, b) =>
a.length === b.length && a.every((n, i) => Object.is(n, b[i]));
const includes = (arrs, search) =>
arrs.some(n => isEqual(n, search));
console.log(includes(array, [ 21, 81 ]));
. const newArr2 = arr2.filter(n => arr1.some(m => m === n.name));
// или
const obj2 = arr2.reduce((acc, n) => (
(acc[n.name] = acc[n.name] ?? []).push(n),
acc
), {});
const newArr2 = arr1.flatMap(n => obj2[n] ?? []);
// или
const newArr2 = [];
for (const n of arr2) {
for (const m of arr1) {
if (m === n.name) {
newArr2.push(n);
break;
}
}
}
arr2.reduceRight((_, n, i, a) => ~arr1.indexOf(n.name) || a.splice(i, 1), null);
// или
arr2.splice(0, arr2.length, ...arr2.filter(function(n) {
return this.has(n.name);
}, new Set(arr1)));
// или
arr2.length -= arr2.reduce((acc, n, i, a) => (
a[i - acc] = n,
acc + !arr1.includes(n.name)
), 0);
const result = graphData.map((n, i) => ({ ...n, color: palette[i % palette.length] }));
const combine = (arr, keys, values) =>
arr.map(({ ...n }, i) => (
values.forEach((m, j) => n[keys[j]] = m[i % m.length]),
n
));
const result = combine(graphData, [ 'color' ], [ palette ]);
const className = 'класс, который не надо удалять';
// Если известно, что класс присутствует или должен быть добавлен в случае отсутствия:
element.className = className;
// Если известно, что класс отсутствует и не должен быть добавлен:
element.className = '';
// Если неизвестно, присутствует ли класс и в случае отсутствия он не должен быть добавлен:
element.className = element.classList.contains(className) ? className : '';
// или
element.classList.remove(...[...element.classList].filter(n => n !== className));