'JavaScript, get one object key name knowing the second [duplicate]
Let's say I have an object that has two key-values:
let object = {name: "abc", id: 12}
But let's say, I don't know one of the key-names, so:
let object = {<unknown_to_me>: "abc", id: 12}
How can I get the first key name if I know the other one? Their positions
THe first one is reachable via:
object.id
Can I get the other one by positions, the ! operator,...?
Solution 1:[1]
You could do so:
let object = {name: "abc", id: 12};
let knownKeyName = "id";
let objectKeys = Object.keys(object);
let unknownKeyName = objectKeys[objectKeys.length - 1 - objectKeys.indexOf(knownKeyName)];
console.log(unknownKeyName)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
