Interview Questions-Objects

Find number of properties in an object

				
					var student = {
  name: "Anurag Nayak",
  empId: "123",
  1: "test",
  nestedObj: {
    id: 123,
    nestedObj1: {
      nestedObj2: {
        nestedObj3: {
          nestedObj4: {},
        },
      },
    },
  },
  testFunc2: function () {
    console.log(2);
  },
  testFunc3: () => {
    console.log(3);
  },
};

//1.calculate property length -->6
Object.keys(student).length;
				
			

Find the depth of nestedObj inside the student array

				
					//2.Find the nested object depth :->nestedObj --> length
//Object.values(nestedObj).forEach((v) => will loop on values
//typeof v ==> to determine if its object or not
//v==>{nestedObj2: {…}} {nestedObj3: {…}} {nestedObj4: {…}}

var student = {
  name: "Anurag Nayak",
  empId: "123",
  1: "test",
  nestedObj: {
    id: 123,
    nestedObj1: {
      nestedObj2: {
        nestedObj3: {
          nestedObj4: {},
        },
      },
    },
  },
  testFunc2: function () {
    console.log(2);
  },
  testFunc3: () => {
    console.log(3);
  },
};

//***************SOLUTION*************************
let depth = 0;
let depthOfNestedObject = (nestedObj) => {
  Object.values(nestedObj).forEach((v) => {
    if (typeof v === "object") {
      depth++;
      depthOfNestedObject(v);
    }
  });
};

depthOfNestedObject(student.nestedObj);
console.log(`%c ${depth}`, "color:red;font-size:20px");

//output :- 4

				
			

find all the functions in the same object

				
					var student = {
  name: "Anurag Nayak",
  empId: "123",
  1: "test",
  nestedObj: {
    id: 123,
    nestedObj1: {
      nestedObj2: {
        nestedObj3: {
          nestedObj4: {},
        },
      },
    },
  },
  testFunc2: function () {
    console.log(2);
  },
  testFunc3: () => {
    console.log(3);
  },
};

//***************SOLUTION*************************
Object.values(student).forEach((v) => {
  if (typeof v === "function") console.log(v);
});
				
			

Check if object is empty

				
					let obj = {};

if (obj && Object.keys(obj).length === 0) {
  console.log(`%c I am empty`, "color:red;font-size:20px");
}

//Explanation
/*
1. first condition .. to check if it exists 

2.checks keys length if 0 ? empty array doesn't have keys ..so valid
*/
				
			

getPrototypeOf Object

				
					//case 1
const proto = {};
const obj = Object.create(proto);
Object.getPrototypeOf(proto) === Object.prototype;
//true


//case 2
const proto1 = {};
const obj1 = Object.create(proto1);
Object.getPrototypeOf(obj1) === Object.prototype;
//false;

//case 3
const proto2 = {};
const obj2 = Object.create(proto2);
Object.getPrototypeOf(obj2) === proto2;
// true

				
			

How to delete a property in object

				
					let obj = {
  age: 15,
  name: "Rohan",
};

//***************SOLUTION********************
delete obj.age;
console.log(obj);//{name: 'Rohan'}

				
			

Nested destructuring

				
					let employee = {
  name: "Anurag Nayak",
  fullName: {
    firstName: "Anurag",
    lastName: "Nayak",
  },
  code: "empCode-123",
};

//1)***********************

let { fullName } = employee;
console.log(fullName);

//OUTPUT:- {firstName: 'Anurag', lastName: 'Nayak'}


//2)**********Nested Destructuring******************

let { fullName: { lastName }} = employee;
console.log(lastName);

//OUTPUT:-Nayak

//3*************Wrong Name*****************

//this gives you undefined as lastName1 doesnt exist
let fullName: { lastName1 }} = employee;

console.log(lastName1);

//OUTPUT:-undefined
				
			

Nested destructuring - own alias

				
					let employee = {
  name: "Anurag Nayak",
  fullName: {
    firstName: "Anurag",
    lastName: "Nayak",
  },
  code: "empCode-123",
};

let {
  fullName: { lastName: surname },
} = employee;
console.log(surname);
				
			

Guess the output- freeze vs seal

				
					let employee = {
  firstname: "anurag",
  lastname: "nayak",
};

employee.age = 32; //you can add age
console.log(employee);
//////////////////////////

				
			
				
					let employee1 = {
  firstname: "anurag",
  lastname: "nayak",
};

Object.freeze(employee1);
//Uncaught TypeError: Cannot add property age, object is not extensible
// employee1.age = 32;

//index.js:963 Uncaught TypeError: Cannot assign to read only
//property 'firstname' of object '#<Object>'

employee1.firstname = "Ábhishek";
console.log(employee1);
				
			
				
					let employee2 = {
  firstname: "anurag",
  lastname: "nayak",
};

Object.seal(employee2);
//employee2.age = 32; //you cannot add property
//you can change name if you dont add age
employee2.firstname = "Ábhishek";//name you can change
console.log(employee2);
				
			

Leave a Comment