SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Last Item In Object

    Example:

    var myObj = {
    'item1' : 'value',
    'item2' : 'value',
    'item3' : 'value',
    'item4' : 'value',
    'item5' : 'value'
    }

    How would I be able to get the last item in that object (item5 in this case).

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    With objects it can be tricky, so an array of objects is much easier to deal with.

    Code JavaScript:
    var myObj = [
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'}
    ]

    Now you'll just be needing to use the following

    Code JavaScript:
    var lastObj = myObj[myObj.length - 1];

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also write a function to obtain an array of object keys:

    Code:
    keys = function(o) {
    	var a = [];
    	for(var p in o) a.push(p);
    	return a;
    }
    
    lastKey = keys(myObj).pop();

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Are the keys stored in any particular order?

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    With objects it can be tricky, so an array of objects is much easier to deal with.

    Code JavaScript:
    var myObj = [
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'},
      {'item' : 'value'}
    ]

    Now you'll just be needing to use the following

    Code JavaScript:
    var lastObj = myObj[myObj.length - 1];
    Theres a thought.
    Quote Originally Posted by stereofrog View Post
    You can also write a function to obtain an array of object keys:

    Code:
    keys = function(o) {
    	var a = [];
    	for(var p in o) a.push(p);
    	return a;
    }
    
    lastKey = keys(myObj).pop();
    Hmm... Will check this out.
    Quote Originally Posted by pmw57 View Post
    Are the keys stored in any particular order?
    What do you mean?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Well keys aren't stored in alphabetical order. To my recollection they're stored in the order that they were created.

    If you created them in the same order that you expect them to be then all is well, otherwise though, things may not turn out as you expect.

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the order of creation is important, you can set your properties with a method that updates a last-in property for the object.
    But you have to use the method to assign new properties.

    var myObj = {
    return{
    'item1' : 'value',
    'item2' : 'value',
    'item3' : 'value',
    'item4' : 'value',
    'item5' : 'value',
    'lastIn' : 'item5'

    }
    myObj.setValue=function(prop,val){
    this.prop=this.val;
    this.lastIn=prop;
    }

  8. #8
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Well keys aren't stored in alphabetical order. To my recollection they're stored in the order that they were created.

    If you created them in the same order that you expect them to be then all is well, otherwise though, things may not turn out as you expect.
    I don't see the connection.

    Quote Originally Posted by mrhoo View Post
    If the order of creation is important, you can set your properties with a method that updates a last-in property for the object.
    But you have to use the method to assign new properties.

    var myObj = {
    return{
    'item1' : 'value',
    'item2' : 'value',
    'item3' : 'value',
    'item4' : 'value',
    'item5' : 'value',
    'lastIn' : 'item5'

    }
    myObj.setValue=function(prop,val){
    this.prop=this.val;
    this.lastIn=prop;
    }
    I am lost here too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •