Search This Blog

2023/04/04

Useful sample of js | 役立つJSサンプル

Get longest string length in array | 配列に一番長い文字の長さを取得

Math.max(...(array.map(el => el.name.length)))

ei: const lengthNameColumn = Math.max(...(array.map(el => el.name.length))) * 10;


8digs number to date format | 8桁数字から日付フォーマットに

'20230901'.replace(/(\d{4})(\d{2})(\d{2})/g, '$1/$2/$3');

'20230901'.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3');


merge two array by key | キーで二つ配列のマージ

data1.forEach(item => map.set(item.key, item));

data2.forEach(item => map.set(item.key, { ...map.get(item.key), ... item }));