getting the last item in a javascript object

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

    You can try this. This will store last item. Here need to convert obj into array. Then use array pop() function that will return last item from converted array.

    var obj = { 'a' : 'apple', 'b' : 'banana', 'c' : 'carrot' };
    var last = Object.keys(obj).pop();
    console.log(last);
    console.log(obj[last]);

提交回复
热议问题