Created
August 16, 2022 04:36
-
-
Save neilkuan/158c149cdcb148f50606c76f02cbbf23 to your computer and use it in GitHub Desktop.
js-objects-fn-tips.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] == '123456789012' ? 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' ] | |
//] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment