Object with for-in loop:
const obj = {
Bangladesh: "Dhaka",
India: "New Delhi",
Pakistan: "Islamabad",
Srilanka: "Colombo",
Nepal: "Kathmandu",
Bhutan: "Thimphu",
};for (const key in obj) {// Here key will return the key from the objectconsole.log(key);// --- Result: ---// Bangladesh// India// Pakistan// Srilanka// Nepal// Bhutan// This will return the values from the objectconsole.log(obj[key]);// --- Result: ---// Dhaka// New Delhi// Islamabad// Colombo// Kathmundu// Thimpu}// values() method will extract the values and// return an array of the valuesconsole.log("VALUES ===> ", Object.values(obj));// keys() method will extract the keys and// return an array of the keysconsole.log("KEYS ===> ", Object.keys(obj));
Post a Comment