- document.addEventListener('mousemove', () => action('action'));
+ document.addEventListener('mousemove', action);
const string = '<tr><td>11</td><td>22</td></tr><tr><td>33</td><td>44</td></tr>';
const match = string.match(/(\d+)(?=<\/td><\/tr>)/g);
// или так: string.match(/(\d+)(?=[^\d]+?<\/tr>)/g)
// или так: string.match(/(\d+)(?=\D+?<\/tr>)/g)
console.log(match); // [ '22', '44' ]
const array = [
[
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
{ name: 'A', age: 10 },
{ name: 'B', age: 10 },
],
[
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
{ name: 'C', age: 10 },
{ name: 'D', age: 10 },
],
];
const groupBy = (collection, extractKey) => {
const cache = new Map();
return collection.reduce((accumulator, entry) => {
const key = extractKey(entry);
if (!cache.has(key)) {
const group = [];
cache.set(key, group);
accumulator.push(group);
}
cache.get(key).push(entry);
return accumulator;
}, []);
};
const newArray = array.map(entry => groupBy(entry, item => item.name));
console.log(newArray);
/*
[
[
[ { name: 'A', age: 10 }, { name: 'A', age: 10 } ],
[ { name: 'B', age: 10 }, { name: 'B', age: 10 } ]
],
[
[ { name: 'C', age: 10 }, { name: 'C', age: 10 } ],
[ { name: 'D', age: 10 }, { name: 'D', age: 10 } ]
]
]
*/
const groupBy = (collection, extractKey) => {
const cache = new Map();
for (const entry of collection) {
const key = extractKey(entry);
if (!cache.has(key)) {
cache.set(key, []);
}
cache.get(key).push(entry);
}
return [...cache.values()];
};
const api = (isError = false, callback) => !isError
? callback(null, { code: 200 })
: callback(new Error(), null);
const promisify = (handle = () => {}) => (...args) => new Promise((resolve, reject) => {
handle(...args, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
promisify(api)(true).then(data => {
console.log('data', data);
}).catch(error => {
console.log('error', error);
});
const formData = new FormData();
for (const key in obj) {
formData.append(key, obj[key]);
}
...
xhr.send(formData);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
fetch
. const url = new URL(location.href)
url.searchParams.set('session', '1')
history.pushState(null, document.title, url.toString())
const params = new URLSearchParams(location.search);
params.set('session', '1');
location.search = params.toString();
a == 1 && a == 2 && a == 3 // true
true
, и если не знать как работает ==
, можно было бы подумать, что это некоторое шаманство. Суть в том, что ==
преобразует типы (а также вызывает valueOf
). Зная правило с valueOf
, переменная a
была равна следующему:const a = {
value: 1,
valueOf() {
return this.value++;
}
};
===
(ну или почти всегда). $(window).scroll(function () {
let ratio = $(document).scrollTop() / (($(document).height() - $(window).height()) / 100);
+ $("#progressbar").text(ratio + "%");
$("#progressbar").width(ratio + "%");
});
const comparator = (a, b) => {
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
} else {
return a.toString().localeCompare(b.toString());
}
};
const isSorted = array => {
const count = array.length;
return array.every((current, index) => {
if (index + 1 < count) {
const next = array[index + 1];
return comparator(current, next) <= 0;
} else {
return true;
}
});
};