Implement a function to read a field inside a nested object | Frontend Problem Solving | JavaScript Interview Question
@Devtools Tech

In this question, you need to implement a function read that takes two parameters:

read(collection, property)

  1. collection: The top level parent object in which we need to find the field.
  2. property: The path of the field we need to find/read.

Expected Output: field value if field exists else undefined.

const collection = {
  a: {
    b: {
      c: {
        d: {
          e: 2
        }
      }
    }
  }
}

// should return 2
read(collection, 'a.b.c.d.e');

// should return undefined
read(collection, 'a.b.c.f');

Loading IDE...

https://www.devtools.tech/