getting the last item in a javascript object

前端 未结 14 1979
遇见更好的自我
遇见更好的自我 2020-12-04 10:45

If I have an object like:

{ \'a\' : \'apple\', \'b\' : \'banana\', \'c\' : \'carrot\' }

If I don\'t know in advance that the list goes up

14条回答
  •  北海茫月
    2020-12-04 11:37

    The other answers overcomplicate it for me.

    let animals = {
      a: 'dog',
      b: 'cat',
      c: 'bird'
    }
    
    let lastKey = Object.keys(animals).pop()
    let lastValue = animals[Object.keys(animals).pop()]
    

提交回复
热议问题