0

I have an object that I am iterating through using JQuery's each function. However, the solution posted in this stack overflow post doesn't work when I tried using the length property. I got undefined in the console when I tried getting a length property value, which I believe this is because I am iterating through an object and not an array.

My code:

$.each(attributes, function(key, value) {
        attrKey = key;
        attrVal = value;
        console.log(attributes.length); //returns undefined
       //do something if it is the last element
 });
Community
  • 1
  • 1
Ron I
  • 4,090
  • 8
  • 33
  • 64
  • Objects don't have a length property...that is why you get undefined, try `Object.keys(attributes).length` – JonH Apr 27 '16 at 19:41
  • You should be using Javascripts native `for...in`, jQuery is a bit overkill for something like this – Derek Pollard Apr 27 '16 at 19:42
  • yes, this is why I don't know how to check if it is the last element, because other posts use the length to check if it is the last element. Since length wont work in this instance, how does one check for the last iteration? – Ron I Apr 27 '16 at 19:43
  • Show us what `attributes` are, you can log it in console: `console.log(typeof attributes, attributes);` – Przemysław Melnarowicz Apr 27 '16 at 19:43
  • What do you mean by last element of an object? – j08691 Apr 27 '16 at 19:43
  • @PrzemysławMelnarowicz typeof returns 'object' – Ron I Apr 27 '16 at 19:44
  • 1
    Check out this post for alternatives to get the number of attributes of your object: http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip – mahi-man Apr 27 '16 at 19:44
  • **sigh** did I not just post this as the first comment. – JonH Apr 27 '16 at 19:45
  • @j08691 The object contains the attributes of an html element, console.log(attributes); returns: Object {styli: "action:add; cat:buttons; name:green button", data: "test-data", class: "testElementContainer test-class"} – Ron I Apr 27 '16 at 19:46
  • @PaulFrench - thanks works perfect! – Ron I Apr 27 '16 at 19:49
  • You can create variable `var lastAttribute` before `$.each` loop, overwrite it each time by current object (`lastAttribute = $(this)`) and put code after `$.each` loop to process that last object. If you don't wanna try other solutions that could be quite simple. – Przemysław Melnarowicz Apr 27 '16 at 19:49

3 Answers3

2

Try plain Javascript instead:

for (var key in attributes) {
  var value = attributes[key];
  // process key,value...
}

Edit:

If you're trying to get the last key/value in an object, you can't. Javascript objects are unordered, meaning that they do not keep track of when additional key/value assignments are made. If order is important, I would recommend changing attributes be an array of objects, where each object is a single key/value, or use a 3rd party library, like this - https://github.com/trentrichardson/Ordering.

If you'd like to get the number of keys in attributes, use Object.keys:

Object.keys(attributes).length
smaili
  • 1,245
  • 9
  • 18
1

Try

var index = 1;
$.each(attributes, function(key, value) {
    if(Object.keys(attributes).length == index)
    { 
         // Do something
    }
    index++;
});
0

per @PaulFrench's comment:

length = Object.keys(attributes).length; 
if(n < length) {
         //do something
}
Ron I
  • 4,090
  • 8
  • 33
  • 64