-1

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
ross
  • 2,684
  • 2
  • 13
  • 22
  • 1
    Possible duplicate of [Find by key deep in a nested object](https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-object) – briosheje May 31 '19 at 14:24
  • Why are you turning your objects into arrays without accessing the properties directly? – gbalduzzi May 31 '19 at 14:40

2 Answers2

1

To answer your direct question:

var db = ''
Object.entries(connection).foreach(item => {
  if (item[0] === 'config') db = item[1].database
})

However, you should be able to access the value directly without turning the connection object in an array:

connection.config.database
gbalduzzi
  • 9,356
  • 28
  • 58
0
let config = connection.filter(([key, value]) => key === "config")[0][1]
let database = config.database // dbname
Jean-Alphonse
  • 800
  • 4
  • 10