简体   繁体   English

使用JQUERY的对象中的下一个和上一个项目

[英]Next and previous item in an object with JQUERY

I have an object "data" as defined: 我有一个定义的对象“数据”:

MYLIST
    61:
        ALREADY_EXIST: 1
        ARCHIVE: 0
        CODE_UNIT: "F.5"
        GROUP_TYPE: "Group"
        ID: 61
        SG_CODE: "ABCD00"
    62:
        ALREADY_EXIST: 0
        ARCHIVE: 0
        CODE_UNIT: "D.1"
        GROUP_TYPE: "Sub-Group"
        ID: 62
        SG_CODE: "ABCD11"
    86:
        ALREADY_EXIST: 1
        ARCHIVE: 0
        CODE_UNIT: "B.1"
        GROUP_TYPE: "Group"
        ID: 61
        SG_CODE: "ABC235"
    99:
        ALREADY_EXIST: 0
        ARCHIVE: 0
        CODE_UNIT: "A.2"
        GROUP_TYPE: "Group"
        ID: 62
        SG_CODE: "ABCD11"
SUBJECT_CODE_NUMBER: 4

I'm trying to parse this object for displaying value following the previous and nex values. 我试图解析此对象以显示先前值和nex值之后的值。

I would like to retrieve the next and previous element in variables , how can I do that? 我想检索变量中的下一个和上一个元素 ,该怎么做?

$.each(data.MYLIST,function(k,v){       
    alert(v); //OK
    console.log(data.MYLIST[k].SG_CODE); //OK
    console.log(????); // FOR THE NEXT VALUE
    console.log(????); // FOR THE PREVIOUS VALUE                        
});

In Mylist k is not a continuous value. 在“我的清单”中,k不是连续值。 k+1 is not compulsory the following value of k. k + 1不是k的强制值。 For example you have the item 62 and after the item is 86. 例如,您有项目62,而项目之后是86。

Thus I cannot use: 因此,我不能使用:

var next = data.EXPERT_GROUP[k+1].SG_CODE  
var previous= data.EXPERT_GROUP[k-1].SG_CODE 

In this case do you know a way for getting the next value and the previous value? 在这种情况下,您知道获取下一个值和上一个值的方法吗? Thank you in advance for your help. 预先感谢您的帮助。

Seb 塞布

With jQuery $.each you can access to the index directly with the first parameter of the function. 使用jQuery $.each您可以直接使用函数的第一个参数访问索引。 Assuming your index can't be negative. 假设您的索引不能为负。

var previous = -1;
$.each(data.MYLIST,function(k,v){
    alert(v); //OK

    if(previous != -1){
      console.log(data.MYLIST[previous].SG_CODE); // FOR THE PREVIOUS VALUE
    }
    console.log(data.MYLIST[k].SG_CODE); //OK
    previous = k;                     
});

Should do the trick for the previous one, don't think you can access next value if your index does not increment on a regular basis. 应该做上一个技巧,如果索引不定期增加,不要认为您可以访问下一个值。

Since your array does not have defined values at continuous index locations, you may define a method like: 由于数组在连续索引位置没有定义的值,因此您可以定义如下方法:

function seekNext(a, index) { 
   for(var i = index; i<a.length; i++){
     if(a[i]){ 
       return a[i] }
     }
};

You may invoke this method as seekNext(data.MYLIST, k) 您可以将此方法作为seekNext(data.MYLIST, k)调用

Same way one may write seekPrev implementation too. 同样也可以编写seekPrev实现。 If the index values of the array are continuous, one could have accessed previous and next values by incrementing and de-incrementing the current array loop index value. 如果数组的索引值是连续的,则可以通过增加和减少当前数组循环索引值来访问上一个和下一个值。

Edit 28/4/17 编辑28/4/17

I overwrite the $.each method. 我覆盖了$.each方法。

code

a= {"a":{"c":"1"},"b":{"c":"2"},"c":{"c":"3"},"d":{"c":"4"},"e":{"c":"7"},"f":{"c":"9"}};

$.each = function (obj, callback) {
  var length, i;
  this.preAndNext = 0,
    this.previous = { "index": null, "data": null },
    this.now = { "index": null, "data": null };
  if (obj.length) {
    length = obj.length;
    for (; i < length; i++) {
      if (callback.call(obj[i], i, obj[i]) === false) { break; }
    }
  } else {
    //that's your object need it.
    for (i in obj) {
      if (this.preAndNext) {
        if (callback.call(obj[i], this.now.index, this.previous.data, this.now.data, obj[i]) === false) { break; }
      }
      this.previous = (this.now.data) ? this.now : this.previous;
      this.now = { "index": i, "data": obj[i] };
      this.preAndNext = true;
    }
    callback.call(obj[i], i, (this.now.data ? this.previous.data : null), (this.now.data || this.previous.data), null);
  }
  return obj;
}

$.each(a,function(i,pre,now,next){
  //i:index
  //pre:previous object;
  //now:now state object;
  //next: the next object;
  console.log(i,pre,now,next);
});

Add your code after you call jquery.If your loop encounter first or last that element was rendered null .(ie the first loop pre = null ) 在调用jquery之后添加代码。如果您的循环遇到第一个或最后一个,则将该元素呈现为null (即,第一个循环pre = null

Hope to help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM