user817507
user817507

Reputation:

Iterate through all the properties of an object skipping first one

Is there any method to skip iterating first element like gt(0) in jQuery
This is the code I am using

 for(var prop in item){
   alert(prop + " = " + item[prop]);
 }

Upvotes: 3

Views: 5166

Answers (5)

Pencho Ilchev
Pencho Ilchev

Reputation: 3241

You shouldn't rely on the order of the properties of the JavaScript objects because the properties are unordered. JQuery's gt works on an array and if you want to achieve something similar, you need the re-design your JSON model, something like:

 var item = [
{
   prop: "value"
},
{
   prop1: "value2"
}];

After wrapping your object properties in an array you can use it like this:

var i = item.length - (item.length - 1);

for (i; i < item.length; i++) {
    for (var k in item[i]) {
        alert(k + "=" + item[i][k]);
    }

}

Here is a fiddle

Upvotes: 2

abuduba
abuduba

Reputation: 5042

I wrote a function which is able to iterate by indexes of properties definded in parameters of function. Two first arguments are such as parameters in slice/substr. These parameters aren't mandatory. We'll had able to calling by:

 someObject.customIterate( callback );

That means ->from = 0, to = -1 iterate through whole properties

 someObject.customIterate( from, callback)

That means iterate from value of from property to the end.

Object.prototype.customIterate = function( from, to, callback ) {

   var f, t;
   if( ( f = typeof from === "function" ) ||  ( t = typeof to === "function" ) ) {
        callback = f ? from : to;
        to = -1;
        from = f ? 0 : from;

    }else if( !callback ) {
        return ;
    }

   for( var i in this) {
        if( from-- > 0 ) {
           continue;
        } else if( to-- ) {
            callback.call( this, i, this[i] );
        } else break;
    }
};

Callback gets two arguments - name of property and its value. Callback is invoked in the context of this which is object from which customIterate was called on.

And now:

var item = { a: 0, b: 1, c: 2, d: 4 };

item.customIterate( 1, function( prop, value ) {
   // if( this.hasOwnProperty( prop ) ) ;// if you need that
        alert( prop + "=" + value );


});

Demo on: http://jsfiddle.net/hLhR9/

Upvotes: 0

balach
balach

Reputation: 97

straight solution: you can use continue in js loops to skip an iteration/element. if (index == 0) continue.

but definitely consider what people have said above.

Upvotes: 0

NiKo
NiKo

Reputation: 11412

Just use Object.hasOwnProperty:

var item = {foo: 'bar', bar: 'baz'};

if (item.hasOwnProperty('foo')) {
    console.log(item['foo']);
}

You could also test for keys using Object.keys:

if (Object.keys(item).indexOf('foo') !== -1) {
    console.log(item['foo']);
}

Upvotes: 1

Stanley Stuart
Stanley Stuart

Reputation: 1145

if (prop !== item[0]){
  if (item.hasOwnProperty(prop){
    alert(prop + " = " + item[prop]);
  }
}

Placing that inside your for in loop should take care of that for you. Please note that you will want to use object.hasOwnProperty to make sure you are not iterating over functions or other objects inherited from the prototype chain.

Note that when using for in loops in JavaScript:

The ECMA standard does not specify an enumeration order but the de facto standard for non-array objects is to enumerate properties according to the order of their original assignment.

via http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/

Upvotes: 0

Related Questions