i want to get the first element of a Object! the object looks like this:
const obj = {
"val1": "test"
}
I already tried this:
obj[0]
but this is not working out for me :/
i want to get the first element of a Object! the object looks like this:
const obj = {
"val1": "test"
}
I already tried this:
obj[0]
but this is not working out for me :/
You can get the value like obj['val1']
. It's not an array. If you want to get in your style then you have to make it an array.
const values = Object.values(obj);
console.log(values[0]);
Demo:
const obj = {
"val1": "test"
};
const values = Object.values(obj);
console.log(values[0]);
You can get the first key/value by,
const obj = {
"val1": "test"
}
key = Object.keys(obj)[0];
value = obj[Object.keys(obj)[0]]
console.log(key, value)