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

后端 未结 6 2206
长情又很酷
长情又很酷 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:31

    You could put the logic for the last item outside the loop:

    var last_item = null;
    for(key in obj) {
      last_item = key;
    }
    console.log(last_item);
    

提交回复
热议问题