// helpers start
const compose = (fn1, fn2) => x => fn1(fn2(x));
const reduceByProp = prop => a =>
a.reduce((acc, curr) => ({ ...acc, [curr[prop]]: curr }), {});
const uniqByProp = prop => compose(Object.values, reduceByProp(prop));
// helpers end
// logic start
const uniqByLastname = uniqByProp('lastname')
const getUniqAndPrint = compose(console.log, uniqByLastname)
// logic end
// exec start
const arr = [
{
'name': 'anna',
'lastname': 'petrova'
},
{
'name': 'vika',
'lastname': 'ivanova'
},
{
'name': 'julia',
'lastname': 'petrova'
},
];
getUniqAndPrint(arr);
// exec end
const obj = [{
'name': 'anna',
'lastname': 'petrova'
},
{
'name': 'vika',
'lastname': 'ivanova'
},
{
'name': 'julia',
'lastname': 'petrova'
},
];
const res = obj.reduce((o, i) => {
if (!o.find(v => v.lastname == i.lastname)) {
o.push(i);
}
return o;
}, []);
console.log(res)
const [a, b, c, d, f, g= ''] = value.match(/\d+/g)!;
const [a, b, c, d, f, g= ''] = value.match(/\d+/g) || [];
match
может вернуть null
и тогда произойдёт ошибка во время исполнения.const [a, b, c, d, f, g= ''] = value.match(/\d+/g) || [];
Либо, если ты на 146% уверен, что match
обязательно что-нибудь найдёт: const [a, b, c, d, f, g= ''] = value.match(/\d+/g) as string[];
const parseToDate = string => {
const [day, month, year, hour, minute] = string.match(/\d+/g).map(match => parseInt(match));
const normalizedYear = year < 2000 && year.toString().length <= 2 ? year + 2000 : year;
const date = new Date(normalizedYear, month - 1, day, hour || 0, minute || 0);
return date;
};
const strings = [
'01 02 2003 04 05',
'01 02 03 04 05',
'01/02/03 04 05',
'01-02-3 04-05',
'23-08/2019 3 4',
'1 1 1980 0 0',
'2 2 0'
];
for (const string of strings) {
const date = parseToDate(string);
console.log(date.toLocaleString());
}
/**
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'01.02.2003, 04:05:00'
'23.08.2019, 03:04:00'
'01.01.1980, 00:00:00'
'02.02.2000, 00:00:00'
*/