1

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,...?

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
NoDiggityNoDoubt
  • 341
  • 1
  • 3
  • 18

1 Answers1

0

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)
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65