0

I have the following json file

[{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},
{"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},
{"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},
{"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}]

I want to add new items using JavaScript, jquery, to end up with

[{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},
{"id":5,"num":"n61","mov_date":"2019-02-02T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-03T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-04T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},
{"id":5,"num":"n61","mov_date":"2019-02-06T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-07T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},
{"id":5,"num":"n61","mov_date":"2019-02-09T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-10T00:00:00","orders":0},
{"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}]

maybe by calculating the number of missed items between dates, or just calculating the diff between the numbers represents the day i.e: "2019-02-01T00:00:00" and "2019-02-05T00:00:00" then add 3 items?

peak
  • 105,803
  • 17
  • 152
  • 177
Hisham
  • 65
  • 1
  • 2
  • 11

4 Answers4

1

var items = [
  {"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},
  {"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},
  {"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},
  {"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}
]

var newItems = []

for(var i = 0; i < items.length; i++){
        newItems.push(items[i])
  
 var currentDay = moment(items[i].mov_date)
        var nextDay = currentDay.add(1, 'days');
 
 if(typeof items[i+1] !== 'undefined'){
  var diff = moment(items[i+1].mov_date).diff(currentDay, 'days')
  
  for(var j = 1; j <= diff; j++){
   var newItem = JSON.parse(JSON.stringify(items[i]))
   newItem.mov_date = moment(items[i].mov_date).add(j, 'days').utc(false).format();
   newItem.orders = 0
   newItems.push(newItem)
  }
 }
}

console.log(newItems)
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
vmf91
  • 1,897
  • 19
  • 27
  • Thank you for your answer, but if you notice there is something wrong i.e the dates of the first 3 inserted items is not correct: "2019-02-02T22:00:00Z" , "2019-02-`04`T22:00:00Z" , "2019-02-`07`T22:00:00Z" while they must be "2019-02-02T22:00:00Z", "2019-02`-03`T22:00:00Z", "2019-02-`04`T22:00:00Z" .. and so on – Hisham Feb 12 '19 at 21:12
  • Sorry, I have corrected my snippet. Now everything is working as expected. – vmf91 Feb 12 '19 at 21:23
0

You could use a Date object which you increment using its setDate method, and which you render to string with toJSON. Then when the date string matches the next entry, you copy it, otherwise you duplicate it with orders: 0:

const data = [{"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19},{"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12},{"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5},{"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7}];

const end = new Date(data[data.length-1].mov_date + "Z");
const result = [];
for (let dt = new Date(data[0].mov_date+"Z"), i = 0; dt <= end; dt.setUTCDate(dt.getUTCDate()+1)) {
    result.push({...data[i], ...(dt.toJSON().slice(0,19) === data[i].mov_date ? (i++, {}) : { orders: 0 })});  
}
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
0

Check also this one that uses reduce() preserving all the props(including id) and only resetting orders and setting correct mov_date in between.

var items = [{
    "id": 5,
    "num": "n61",
    "mov_date": "2019-02-01T00:00:00",
    "orders": 19
  },
  {
    "id": 5,
    "num": "n61",
    "mov_date": "2019-02-05T00:00:00",
    "orders": 12
  },
  {
    "id": 5,
    "num": "n61",
    "mov_date": "2019-02-08T00:00:00",
    "orders": 5
  },
  {
    "id": 5,
    "num": "n61",
    "mov_date": "2019-02-11T00:00:00",
    "orders": 7
  }
]

const newItems = items.reduce((acc, next) => {

  // first run with early return
  if (!acc.length) {
    return [...acc, next]
  }

  // taking the recent item, to preserve the id and other props
  const prevItem = acc[acc.length - 1];

  // getting diff in days - 1
  const days = moment.utc(next.mov_date).diff(moment.utc(prevItem.mov_date), 'days') - 1;

  // [...Array] is a trick to get mappable arrays without array holes, 
  // but with initialized undefined values, 
  // so we can get the index during map
  const inBetweenValues = [...Array(days)].map((_, dayIndex) => {
    return {
      ...prevItem,
      orders: 0,
      mov_date: moment.utc(prevItem.mov_date).add(dayIndex + 1, 'days').format()
    };
  });

  // merging it all, and moving to the next loop
  return [...acc, ...inBetweenValues, next];
}, [])

console.log(newItems);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35
0

Here is an algorithm that will do That for you. I used Convert JS date time to MySQL datetime for date conversion.

function twoDigits(d) {
    if(0 <= d && d < 10) return "0" + d.toString();
    if(-10 < d && d < 0) return "-0" + (-1*d).toString();
    return d.toString();
}

function toMysqlFormat() {
    return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + "T" + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};


var prev = 0;

for( var x = 1; x < obj.length; x++ ){

    if( !obj[x -1].mov_date ){
        continue;
    }

    var tx = Date.parse( obj[x-1].mov_date );
    var diff = ( Date.parse(obj[x].mov_date ) - tx ) / (1000*24*60*60);

    for( var y = 1; y < diff; y++ ){

        obj.splice( x - 1 + y,0, { "id" : 5, "num" : "n61", "mov_date" : toMysqlFormat.bind( new Date( tx + ( y*1000*24*60*60) ) )(), "orders" : 0} );

    }
    x += diff - 1;

}

for( var x = 0; x < obj.length; x++ ){
    console.log( JSON.stringify( obj[x] ) );
}

/* Result :
/* {"id":5,"num":"n61","mov_date":"2019-02-01T00:00:00","orders":19} */
/* {"id":5,"num":"n61","mov_date":"2019-02-02T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-03T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-04T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-05T00:00:00","orders":12} */
/* {"id":5,"num":"n61","mov_date":"2019-02-06T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-07T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-08T00:00:00","orders":5} */
/* {"id":5,"num":"n61","mov_date":"2019-02-09T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-10T00:00:00","orders":0} */
/* {"id":5,"num":"n61","mov_date":"2019-02-11T00:00:00","orders":7} */
*/