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
Very tricky, iteration through object does not necessarily guarantees order. ES2015 provides several methods that are iterating through symbol\string type keys by following the creation order.
Assuming last key which has a value is the last entry that was defined in the object with truthy value:
function getLastKeyWithTruthyValue(obj) {
Object.getOwnPropertyNames(obj).filter(key => !!obj[key]).slice(-1)[0]
}
filter(Boolean)
can be a cool method to filter, as described in this cool answer. If any value which is not undefined is required, use val => val !== undefined
as the filter callback.