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

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

    You could loop through all of them and save the last one in a variable.

    var lastItem = null;
    for(key in obj) {
      console.log( key + ' has a value ' + obj[key] );
      lastItem = key;
    }
    // now the last iteration's key is in lastItem
    console.log('the last key ' + lastItem + ' has a value ' + obj[lastItem]);
    

    Also, because of how JavaScript works the key is also in your loop's key variable, so the extra variable is not even needed.

    for(key in obj) {
      console.log( key + ' has a value ' + obj[key] );
    }
    // now the last iteration's key is in key
    console.log('the last key ' + key + ' has a value ' + obj[key]);
    
    0 讨论(0)
  • 2021-02-07 06:23

    don't use for (key in obj), it will iterate over all enumerable properties including prototype properties, and can lead to amazingly horrible things. Modern JS has a special function for getting only the relevant keys out of an object, using Object.keys(...), so if you use var keys = Object.keys(obj) to get the list of keys as an array, you can then iterate over that:

    // blind iteration
    Object.keys(obj).forEach(function(key, i) {
      var value = obj[key];
      // do what you need to here, with index i as position information.
      // Note that you cannot break out of this iteration, although you
      // can of course use ".some()" rather than ".forEach()" for that.
    });
    
    // indexed iteration
    for(var keys = Object.keys(obj), i = 0, end = keys.length; i < end; i++) {
      var key = keys[i], value = obj[key];
      // do what you need to here, with index i as position information,
      // using "break" if you need to cut the iteration short.
    });
    

    or select its last element immediately

    var keys = Object.keys(obj);
    var last = keys[keys.length-1];
    

    or using a slice:

    var keys = Object.keys(obj);
    var last = keys.slice(-1)[0];
    

    or using a shift (but that's a destructive operation, so we're not caching the keys because the shift turns it into "not all the keys anymore"):

    var last = Object.keys(obj).shift();
    
    0 讨论(0)
  • 2021-02-07 06:25
    for(var x=0 ; x<Object.keys(obj).length ; x++)
    {
         if(x==Object.keys(obj).length-1) // code for the last iteration
    
    }
    

    Or could use Object.size(obj)

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-07 06:32

    just shorter

    var last = (last=Object.keys(json))[last.length-1];
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题