xxxxxxxxxx
var example = {
foo1: { /* stuff1 */},
foo2: { /* stuff2 */},
foo3: { /* stuff3 */}
};
let [first] = Object.keys(example)
console.log(first)
xxxxxxxxxx
var obj = { "a" : 1, "b" : 2, "c" : 3};
alert(Object.keys(obj)[0]);
// alerts "a"
let objLength = Object.keys(obj).length
console.log(objLength)
// Logs 3 as "obj" has 3 keys
var objValue = Object.values(obj)[0]
console.log(objValue)
// Logs "1" as the value of the first key(a) is "1"
xxxxxxxxxx
var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]]; //returns 'someVal'
xxxxxxxxxx
var person = {"name":"Steve","age":43};
var firstKeyValue = person[Object.keys(person)[0]]; //"Steve"
xxxxxxxxxx
// Sample object
const myObject = {
1: 'One',
2: 'Two',
3: 'Three'
};
// Retrieve the first key
const firstKey = Object.keys(myObject)[0];
console.log(firstKey); // Output: "1"