-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?
5
  • stackoverflow.com/a/38528645/7008628 => use reduce to insert your date + log object Commented Jun 2, 2021 at 11:49
  • use array.push() Commented Jun 2, 2021 at 11:49
  • 1
    Question is unclear. ...after each new date in the now sorted array what is a 'new date'? Commented Jun 2, 2021 at 11:56
  • @Martin another value in the date key Commented Jun 2, 2021 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() Commented Jun 2, 2021 at 13:13

3 Answers 3

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!' }
]
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.