0

I have a JSON Array, which is like this:

0: {stop: "C.-D.-Friedrich Straße", time: "04:38:00", realtime: "04:39:00"}
1: {stop: "Zellescher Weg", time: "04:40:00", realtime: "04:40:00"}
2: {stop: "SLUB", time: "04:41:00", realtime: "04:41:00"}
3: {stop: "TU Dresden", time: "04:42:00", realtime: "04:41:00"}
4: {stop: "Nürnberger Platz", time: "04:43:00", realtime: "04:42:00"}
5: {stop: "Bernhardstraße", time: "04:44:00", realtime: "04:43:00"}
6: {stop: "Chemnitzer Straße", time: "04:45:00", realtime: "04:44:00"}
7: {stop: "Löbtau", time: "04:47:00", realtime: "04:47:00"}

This kind of structure repeats 210 times in my JSON Array:

enter image description here

Now I want to add to every stops-Array two new items like:

0: {stop: "C.-D.-Friedrich Straße", time: "04:38:00", realtime: "04:39:00", diff_minute: "1", diff_hour: "0"}

my try:

    for (var i = 0; i < trainsData.length; i++){
        for (var j = 0; j < trainsData[i].stops.length; j ++){
            var start = trainsData[i].stops[j].time;
            var end = trainsData[i].stops[j].realtime;

            start = start.split(":");
            end = end.split(":");

            var startDate = new Date(0, 0, 0, start[0], start[1], start[2]);
            var endDate = new Date(0, 0, 0, end[0], end[1], end[2]);

            var diff = endDate.getTime() - startDate.getTime();

            var hours = Math.floor(diff / 1000 / 60 / 60);
            diff -= hours * 1000 * 60 * 60;
            var minutes = Math.floor(diff / 1000 / 60);

            timeDifference = [{diff_minute: minutes, diff_hour: hours}];
            trainsData[i].stops[j].push(timeDifference)
        }
    }

Unfortunately I get the error that trainsData[i].stops[j].push is not a function.

It would be nice if someone could help me.

Sam Toorchi
  • 133
  • 1
  • 9
  • looks like you could use `map`, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – Smern Nov 01 '19 at 16:35
  • 1
    as for the way you are doing and why `stops[j].push` is not a function... `stops[j]` is a plain object, not an array with a `push` function... if you want to add a property you could just do `stops[j]['newProp'] = 'some value'` ... you could also use `Object.extend( stops[j], objWithNewProps)` – Smern Nov 01 '19 at 16:41
  • @Dallas Thank you very much! that is what I'm searching for :) – Sam Toorchi Nov 01 '19 at 16:44
  • Okay, voted to close as duplicate of [Is it possible to add dynamically named properties to JavaScript object?](https://stackoverflow.com/questions/1184123/is-it-possible-to-add-dynamically-named-properties-to-javascript-object) – Smern Nov 01 '19 at 16:46

1 Answers1

0

You can do:

const trainsData = [{stop: "C.-D.-Friedrich Straße",time: "04:38:00",realtime: "04:39:00"},{stop: "Zellescher Weg",time: "04:40:00",realtime: "04:40:00"},{stop: "SLUB",time: "04:41:00",realtime: "04:41:00"},{stop: "TU Dresden",time: "04:42:00",realtime: "04:41:00"},{stop: "Nürnberger Platz",time: "04:43:00",realtime: "04:42:00"},{stop: "Bernhardstraße",time: "04:44:00",realtime: "04:43:00"},{stop: "Chemnitzer Straße",time: "04:45:00",realtime: "04:44:00"},{stop: "Löbtau",time: "04:47:00",realtime: "04:47:00"}]

const result = trainsData.map((item) => {
  const start = item.time.split(':')
  const end = item.realtime.split(':')

  const startDate = new Date(0, 0, 0, start[0], start[1], start[2])
  const endDate = new Date(0, 0, 0, end[0], end[1], end[2])

  let diff = endDate.getTime() - startDate.getTime()

  const hours = Math.floor(diff / 1000 / 60 / 60)
  diff -= hours * 1000 * 60 * 60
  const minutes = Math.floor(diff / 1000 / 60)
  
  const timeDifference = {
    diff_minute: minutes,
    diff_hour: hours
  }
  
  // new item keys
  return { ...item, ...timeDifference }
})

console.log(result)
.as-console-wrapper {max-height: 100% !important;top: 0;}
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46