简体   繁体   中英

Printing object keys and values in javascript

I have an object and need to print out the object keys and values without using loops or if-else statements. I am allowed to use template literals. (I'm new to JavaScript programming)

let TLOP = { firstName: "John", lastName: "West", age: "28", hairColor: "black" };

I'm not sure if this is considered "statements" by your terms, but this is the only way I can think of without using loops:

let TLOP = { firstName: "John", lastName: "West", age: "28", hairColor: "black" };

console.log(Object.keys(TLOP))
console.log(Object.values(TLOP))

You can do something like this:

let TLOP = { firstName: "John", lastName: "West", age: "28", hairColor: "black" }

let TLOP_keys = Object.keys(TLOP);
let TLOP_entries = Object.values(TLOP);

console.log(TLOP_keys);
console.log(TLOP_entries);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM