-2

Let's say I have an array like so:

var array = [{date: "2021-06-02", name: "Foo" /*... and more*/}]

Now i have sorted the array with

array.sort(function (a, b) {
    var keyA = new Date(a.date),
        keyB = new Date(b.date);
    if (keyA < keyB) return -1; // a.date is before b.date
    if (keyA > keyB) return 1;  // other way around

    return 0;

});

And now I want to add {type: "new date occured", date: "", name: "New Date!"} after each new date in the now sorted array.

How do I do that?
  • https://stackoverflow.com/a/38528645/7008628 => use reduce to insert your date + log object – Nicolas Menettrier Jun 02 '21 at 11:49
  • use `array.push()` – Vivek Bani Jun 02 '21 at 11:49
  • 1
    Question is unclear. `...after each new date in the now sorted array` what is a 'new date'? – Martin Jun 02 '21 at 11:56
  • @Martin another value in the `date` key – CreamyCheese384 Jun 02 '21 at 11:59
  • @CreamyCheese384 Then you must iterate over the existing array while successively building up a second array with each iterated item plus a new separator item whenever you encounter a different date from the previous one. You can implement that with [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) – Martin Jun 02 '21 at 13:13

3 Answers3

0

You can use like following

var array = [{date: "2021-06-02", name: "Foo" }, {date: "2021-06-01", name: "Bar" }, {date: "2021-06-05", name: "Hello" }]
array = array.sort(function (a, b) {
    var keyA = new Date(a.date),
        keyB = new Date(b.date);
    if (keyA < keyB) return -1; // a.date is before b.date
    if (keyA > keyB) return 1;  // other way around

    return 0;

});

console.log(array)

array.push({type: "new date occured", date: "", name: "New Date!"})

console.log(array)
[
  { date: '2021-06-01', name: 'Bar' },
  { date: '2021-06-02', name: 'Foo' },
  { date: '2021-06-05', name: 'Hello' }
]
[
  { date: '2021-06-01', name: 'Bar' },
  { date: '2021-06-02', name: 'Foo' },
  { date: '2021-06-05', name: 'Hello' },
  { type: 'new date occured', date: '', name: 'New Date!' }
]
nkkumawat
  • 693
  • 1
  • 6
  • 9
0

I think this answers your question, though what you actually want to do is very unclear.

const array = [{date: "2021-06-02", name: "Foo" }, {date: "2021-06-01", name: "Bar" }, {date: "2021-06-05", name: "Hello" }];

function sortFunc(a, b) {
    var keyA = new Date(a.date),
        keyB = new Date(b.date);
    if (keyA < keyB) return -1; // a.date is before b.date
    if (keyA > keyB) return 1;  // other way around
    return 0;
}
array.sort(sortFunc);
console.log(array);

function alterDate(name, newDate) {
  const thisDate = array.find(d => d.name === name);
  if (!thisDate) return;
  thisDate.date = newDate;
  thisDate.type = "new date occured";
  array.sort(sortFunc);
}
alterDate("Bar", "2021-06-08");
console.log(array);
LaytonGB
  • 1,384
  • 1
  • 6
  • 20
0

You say "insert after" so here I add one from the date in the array and use that, inserting a new object after the current one for each instance in your array. I created a sample array to show a sample set.

Given some ambiguity of your question this might not be exactly what you desire but should give you a point to work from.

I also took liberty and added a type:'old' to the original array so they all have the same properties - easily removed if you wish just do so.

var myArray = [{
  date: "2021-06-02",
  name: "Foo"
}, {
  date: "2021-06-06",
  name: "bar"
}, {
  date: "2021-06-12",
  name: "fiz"
}, {
  date: "2021-02-21",
  name: "feb"
}];

function compare(firstEl, secondEl) {
  let keyA = new Date(firstEl.date).getTime(),
    keyB = new Date(secondEl.date).getTime();
  if (keyA < keyB) return -1; // firstEl.date is before secondEl.date
  if (keyA > keyB) return 1; // other way around
  //equal
  return 0;
}

function getNewDate(olddate) {
  let nextDay = new Date(olddate);
  nextDay.setDate(nextDay.getDate() + 1);
  let dateString = nextDay
    .toISOString("en-CA")
    .split("T")[0];
  return dateString;
}
myArray.sort(compare);
// doing this just to show the 'old' with the same set of properties
myArray.forEach(function (element) {
  element.type = "old";
});
i = myArray.length - 1;

do {
  let replacecount = 0;
  let currItem = myArray[i];
  let nextDay = getNewDate(currItem.date);

  let newvalue = {
    type: 'new date occurred',
    date: nextDay,
    name: "newDate" + i
  };
  myArray.splice(i+1, 0, newvalue);
} while (i--)

console.log(myArray);
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100