循环对象并在某个位置之前和之后执行自定义代码

时间:2020-12-25 03:16:25

I'm little bit stuck and hope someone can give me a little help. jQuery and Moment Libs are already in my project.

我有点陷入困境,希望有人能给我一些帮助。 jQuery和Moment Libs已经在我的项目中了。

I got a javascript object (json response from PHP) that is build like this:

我有一个javascript对象(来自PHP的json响应),它构建如下:

0: {hour: "0", id: "1", job: "A", position: "0"}
1: {hour: "2", id: "2", job: "B", position: "1"}
2: {hour: "3", id: "3", job: "C", position: "2"}
3: {hour: "4", id: "4", job: "D", position: "3"}

Now I select a random ID and a DATE TIME let's say 2 and 2015-01-01 12:00.

现在我选择一个随机ID和一个DATE TIME让我们说2和2015-01-01 12:00。

The solution output should be:

解决方案输出应该是:

Job: A, Date: 2015-01-01 10:00 //-2h (hours from Job B) minus from previous ID Date
Job: B, Date: 2015-01-01 12:00 //selected ID 
Job: C, Date: 2015-01-01 15:00 //+3h hours add to last ID Date
Job: D, Date: 2015-01-01 19:00 //+4h hours add to last ID Date

This is my Code so far:

这是我的代码到目前为止:

       var sel_date = moment([2015,00,01,12,00]);
       var sel_id = '2';

       var sel_object = jQuery.grep(e, function(n,i){
           return n.id === sel_id;
       });
       var sel_position = sel_object[0].position;

       var job = [];
       var date = [];
       var add_date = sel_date;

       $.each(e,function(key, value){
           job.push(value.job);
           if (value.position != sel_position){
               if (value.position > sel_position){
                   add_date = moment(add_date).add(Number(value.hour), 'hours').format('YYYY-MM-DD HH:mm');
                   date.push(add_date);
               } else {
                   date.push(moment(sel_date).subtract(Number(value.hour), 'hours').format('YYYY-MM-DD HH:mm'));
               }
           } else {
               date.push(moment(sel_date).format('YYYY-MM-DD HH:mm'));
           }

       })

The problem are the smaller positions B, C, D works but when the position is smaller I would need value.hour from the upcoming loop. Should I run a second loop inside the loop? Cannot think of a good way.

问题是B,C,D的位置较小,但是当位置较小时,我需要从即将到来的循环中获得value.hour。我应该在循环内运行第二个循环吗?想不出一个好办法。

1 个解决方案

#1


0  

sorry could "easy" fix it myself. I just use the object e and get the value from there with one position forward.

抱歉可以“轻松”自己解决。我只是使用对象e并从那里获得一个位置向前的值。

   var sel_date = moment([2015,00,01,12,00]);
   var sel_id = '2';

   var sel_object = jQuery.grep(e, function(n,i){
       return n.id === sel_id;
   });
   var sel_position = sel_object[0].position;

   var job = [];
   var date = [];
   var add_date = sel_date;
   var sub_date = sel_date;   //ANSWER

   $.each(e,function(key, value){
       job.push(value.job);
       if (value.position != sel_position){
           if (value.position > sel_position){
               add_date = moment(add_date).add(Number(value.hour), 'hours').format('YYYY-MM-DD HH:mm');
               date.push(add_date);
           } else {
              //ANSWER
              var loop = sel_position - value.position;
              var save_date = sel_date;
              for ( var x = 0; x < loop; x++) {
                    var sub_hour = e[sel_position-x].hour;
                    save_date = moment(save_date).subtract(Number(sub_hour), 'hours').format("YYYY-MM-DD HH");
                   }
              date = save_date;
           }
       } else {
           date.push(moment(sel_date).format('YYYY-MM-DD HH:mm'));
       }

   })

Cheers Hannes

#1


0  

sorry could "easy" fix it myself. I just use the object e and get the value from there with one position forward.

抱歉可以“轻松”自己解决。我只是使用对象e并从那里获得一个位置向前的值。

   var sel_date = moment([2015,00,01,12,00]);
   var sel_id = '2';

   var sel_object = jQuery.grep(e, function(n,i){
       return n.id === sel_id;
   });
   var sel_position = sel_object[0].position;

   var job = [];
   var date = [];
   var add_date = sel_date;
   var sub_date = sel_date;   //ANSWER

   $.each(e,function(key, value){
       job.push(value.job);
       if (value.position != sel_position){
           if (value.position > sel_position){
               add_date = moment(add_date).add(Number(value.hour), 'hours').format('YYYY-MM-DD HH:mm');
               date.push(add_date);
           } else {
              //ANSWER
              var loop = sel_position - value.position;
              var save_date = sel_date;
              for ( var x = 0; x < loop; x++) {
                    var sub_hour = e[sel_position-x].hour;
                    save_date = moment(save_date).subtract(Number(sub_hour), 'hours').format("YYYY-MM-DD HH");
                   }
              date = save_date;
           }
       } else {
           date.push(moment(sel_date).format('YYYY-MM-DD HH:mm'));
       }

   })

Cheers Hannes