getting the last item in a javascript object

前端 未结 14 1986
遇见更好的自我
遇见更好的自我 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:26

    Solution using the destructuring assignment syntax of ES6:

    var temp = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' };
    var { [Object.keys(temp).pop()]: lastItem } = temp;
    console.info(lastItem); //"carrot"

提交回复
热议问题