I have nested object which holding details of DB server. from the nested object i have to search the key name called 'database' and I have display its value.
I found the solution by calling like this:
var database = (Object.entries((Object.entries(connection))[3]))[1][1].database;
but some time position are getting changed. so could not able to get the value. so, how could i find the key in the nested object to find the value. plz help. thanks in advance.
console.log("connection:", Object.entries(connection) );
Result
connection: [ [ '_events',
[Object: null prototype] { end: [Function: _removeFromPool], error: [Function] } ],
[ '_eventsCount', 2 ],
[ 'config',
ConnectionConfig {
host: 'localhost',
port: 336,
localAddress: undefined,
socketPath: undefined,
user: 'user',
password: 'password',
database: 'dbname',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: [Pool],
ssl: false,
multipleStatements: true,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 521167,
protocol41: true } ],
[ '_socket',
Socket {
connecting: false,
_hadError: false,
_handle: [TCP],
_parent: null,
_host: null,
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
timeout: 0,
[Symbol(asyncId)]: 26,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]:
Timeout {
_called: false,
_idleTimeout: -1,
_idlePrev: null,
_idleNext: null,
_idleStart: 8926,
_onTimeout: null,
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: true,
[Symbol(asyncId)]: 29,
[Symbol(triggerId)]: 25 },
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } ],
[ '_protocol',
Protocol {
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [ConnectionConfig],
_connection: [PoolConnection],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshake: true,
_handshaked: true,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: [HandshakeInitializationPacket],
_parser: [Parser] } ],
[ '_connectCalled', true ],
[ 'state', 'authenticated' ],
[ 'threadId', 2859 ],
[ '_pool',
Pool {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [PoolConfig],
_acquiringConnections: [],
_allConnections: [Array],
_freeConnections: [],
_connectionQueue: [],
_closed: false } ] ];
and what is working now,
var database = (Object.entries((Object.entries(connection))[3]))[1][1].database;
console.log(database);
output: dbname