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 }));



2022/11/29

Add event listen to window focus in and out

var blurFuc = function() {
    console.log(focus out)
}

var focusFuc = function() {
    console.log(focus in)
}

window.addEventListener('blur', blurFunc);
window.addEventListener('focus', focusFunc);

2022/08/29

Convert an Object to an Array in JavaScript

const arr = {

    first: '1',

    second: '2'

};

 

Object.keys(arr)

  • [ 'first', ' second' ]

 

Object.values(arr)

  • [ '1', ' 2' ]

 

Object.entries(arr)

  • [ [ 'first', '1' ], [ ' second ', '2' ] ]

2022/06/22

JS: Sort array by datetime, number/ filter by key

const list = [

        {

            "id": "1",

            "createdDate": "2022/03/24 09:49",

            "count": 15,

            "type": 1,

            "title": "テスト",

            "uploader": "アップロード",

        },

        {

            "id": "7",

            "createdDate": "2022/06/17 11:34",

            "count": 5,

            "type": 3,

            "title": "テスト",

            "uploader": "アップロード",

        },

        {

            "id": "3",

            "createdDate": "2022/06/17 11:00",

            "count": 15,

            "type": 1,

            "title": "テスト",

            "uploader": "アップロード2",

        },

        {

            "id": "31",

            "createdDate": "2022/06/17 12:00",

            "count": 1,

            "type": 2,

            "title": "テスト",

            "uploader": "アップロード2",

        },

    ];


const result = list.find(x => x. id == "3");

const result2 = list.filter(x => x. type == "3");

const result3 = list.filter(x => x. uploader.toUpperCase().includes("アップロード2"));

// new sort(from new to old)

const result4 = list.sort(function(a,b){

                    return new Date(b.createdDate) - new Date(a.createdDate);

                });

// old sort(from old to new)

const result5 = list.sort(function(a,b){

                    return new Date(a.createdDate) - new Date(b.createdDate);

                });

// asc

const result6 = list.sort(function(a,b){

                    return b.relatedCount - a.relatedCount;

                });

// less

const result6 = list.sort(function(a,b){

                    return a.relatedCount - b.relatedCount;

                });



if array is object Array, error will happened as below.

TypeError: Cannot assign to read only property '0' of object '[object Array]'

need to make a copy of the array before sort.

arrayForSort = [...list]

2021/03/22

How to indent text exactly one character width

Set style as below:


padding-left: 1em:

text-indent: 1em;

margin: 0px