I need to get the last key of an object which has a value. So if this would be the object...
const obj = { first: \'value\', second: \'something\', third: undefi
Object properties do have a certain order differing from insertion order that isn't really useful in this case as described by T.J. Crowder. This Stack Overflow question is a good read on the subject, and I recommend reading further than the first answer.
So instead of relying on objects which have no guarantee to order1, use something that does guarantee order. You can use something like a Map, which is similar to an object with key/value pairs, but the pairs are ordered. For example:
const obj = new Map([
["first", "foo"],
["second", "bar"],
["third", undefined],
["fourth", undefined]
]);
const result = Array.from(obj.keys()).reduce((lastKey, currKey) => obj.get(currKey) !== undefined ? currKey : lastKey);
console.log(result);
The Map
constructor takes in an iterable to create a Map
out of. The array of arrays construct a Map
with key and value pairs of the subarrays. Thus, the following Map
is created and stored into obj
:
+----------+-----------+
| Key | Value |
+----------+-----------+
| "first" | "foo" |
| "second" | "bar" |
| "third" | undefined |
| "fourth" | undefined |
+----------+-----------+
Then, the line Array.from(obj.keys()) creates an array from the keys of the Map
which are first
, second
, third
, and fourth
. It then uses Array.prototype.reduce to deduce the last key which has a defined value.
The reduce
callback uses lastKey
which is the accumulator/last key with the defined value, and currKey
which is the current key being processed. It then checks if obj.get(currKey) is not undefined. If it is not undefined, then it is returned and assigned to the accumulator. This goes through the entire array and the final value (accumulator) is returned to result
. The result is the last key that had a defined value.2
1It should be noted that in ES2015, there are a selection of methods that do actually guarantee returning the keys in the insertion order. These include Object.assign
, Object.defineProperties
, Object.getOwnPropertyNames
, Object.getOwnPropertySymbols
, and Reflect.ownKeys
. You can rely on these, instead of using Map
.
2There are many other ways to get the last key. You could filter the array and slice it, like Reuven Chacha did. I think reducing it is more descriptive but some other approaches are more straightforward in operation.