最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterate through all the properties of an object skipping first one - Stack Overflow

programmeradmin3浏览0评论

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]);
 }

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]);
 }
Share Improve this question asked Dec 30, 2011 at 7:02 user817507user817507 0
Add a ment  | 

5 Answers 5

Reset to default 2

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

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']);
}
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./2011/01/04/exploring-javascript-for-in-loops/

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.

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/hLhR9/

发布评论

评论列表(0)

  1. 暂无评论