Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Created August 16, 2022 04:36
Show Gist options
  • Select an option

  • Save neilkuan/158c149cdcb148f50606c76f02cbbf23 to your computer and use it in GitHub Desktop.

Select an option

Save neilkuan/158c149cdcb148f50606c76f02cbbf23 to your computer and use it in GitHub Desktop.

Revisions

  1. neilkuan revised this gist Aug 16, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion js-objects-fn-tips.ts
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ const accountId2 = Object.keys(sts).filter( array => sts[array] === '12345678901

    // find the key when value equal '123456789012';
    let accountId: string[] = [];
    Object.entries(sts).forEach( array => array[1] == '1234567890123' ? accountId.push(array[0]) : false);
    Object.entries(sts).forEach( array => array[1] == '123456789012' ? accountId.push(array[0]) : false);

    console.log(accountId2);
    console.log('*'.repeat(50));
  2. neilkuan created this gist Aug 16, 2022.
    41 changes: 41 additions & 0 deletions js-objects-fn-tips.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@

    interface stsInterface {
    [index: string]: string
    }

    const sts: stsInterface = {
    UserId: "AAAAAanbc:botocore-session",
    Account: "123456789012",
    Arn: "arn:aws:sts::123456789012:assumed-role/abc",
    Account1: "123456789012",
    Account2: "123456789012",
    Account3: "123456789012",
    }
    // find the key when value equal '123456789012';
    // filter() will return Array back.
    // => not give "{}" will return value.
    const accountId2 = Object.keys(sts).filter( array => sts[array] === '123456789012');

    // find the key when value equal '123456789012';
    let accountId: string[] = [];
    Object.entries(sts).forEach( array => array[1] == '1234567890123' ? accountId.push(array[0]) : false);

    console.log(accountId2);
    console.log('*'.repeat(50));
    console.log(accountId);
    //[ 'Account', 'Account1', 'Account2', 'Account3' ]
    //**************************************************
    //[ 'Account', 'Account1', 'Account2', 'Account3' ]

    // console.log(Object.entries(sts))
    //[
    // [ 'UserId', 'AAAAAanbc:botocore-session' ],
    // [ 'Account', '123456789012' ],
    // [
    // 'Arn',
    // 'arn:aws:sts::123456789012:assumed-role/abc'
    // ],
    // [ 'Account1', '123456789012' ],
    // [ 'Account2', '123456789012' ],
    // [ 'Account3', '123456789012' ]
    //]