How to get the last item in a javascript-value object with a for loop?

后端 未结 6 2204
长情又很酷
长情又很酷 2021-02-07 06:11
var obj = { \'a\' : \'apple\', \'b\' : \'banana\', \'c\' : \'carrot\' }

If I do a

for(key in obj) {
  console.log( key + \' has a value         


        
6条回答
  •  盖世英雄少女心
    2021-02-07 06:39

    You could push all of the keys/values into a a empty array variable that you created. Then, access the last element in the array using array.length-1.

提交回复
热议问题