为了账号安全,请及时绑定邮箱和手机立即绑定

比较和删除 javascript 数组中的值

比较和删除 javascript 数组中的值

德玛西亚99 2021-10-29 17:07:52
我有一个数组,里面有一堆对象。一个对象的“endTime”值有时大于前一个对象的“endTime”值。我在数组的第二个对象中添加了一个示例。[  {     title: 'Title 1',    startTime: '2019-09-26T06:00:00+0100',    endTime: '2019-09-26T08:30:00+0100'   },  {     title: 'Title 2',    startTime: '2019-09-26T08:00:00+0100',    endTime: '2019-09-26T08:15:00+0100'   },  {     title: 'Title 3',    startTime: '2019-09-26T08:30:00+0100',    endTime: '2019-09-26T09:25:00+0100'   },  {     title: 'Title 4',    startTime: '2019-09-26T09:25:00+0100',    endTime: '2019-09-26T10:25:00+0100'   },  {     title: 'Title 5',    startTime: '2019-09-26T10:25:00+0100',    endTime: '2019-09-26T11:00:00+0100'   }]'endTime' 应该按时间顺序排列,并且 '8:15' 永远不应该在数组中的 '8:30' 之后。我希望在“endTime”值小于之前的值时进行检查,然后将其从数组中删除。任何帮助,将不胜感激。谢谢
查看完整描述

3 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

这里有两种方法。第一个返回数组,其中项目仅与数组中的前一个对象进行比较。第二个将该项与数组中当前项之前的所有对象进行比较。


const data = [{

    title: 'Title 1',

    startTime: '2019-09-26T06:00:00+0100',

    endTime: '2019-09-26T08:30:00+0100'

  },

  {

    title: 'Title 2',

    startTime: '2019-09-26T08:00:00+0100',

    endTime: '2019-09-26T08:15:00+0100'

  },

  {

    title: 'Title 3',

    startTime: '2019-09-26T08:30:00+0100',

    endTime: '2019-09-26T09:25:00+0100'

  },

  {

    title: 'Title 4',

    startTime: '2019-09-26T09:25:00+0100',

    endTime: '2019-09-26T10:25:00+0100'

  },

  {

    title: 'Title 5',

    startTime: '2019-09-26T10:25:00+0100',

    endTime: '2019-09-26T11:00:00+0100'

  }

].map(item => {

  item.startTime = new Date(item.startTime)

  item.endTime = new Date(item.endTime)

  return item

})


const greaterThanLast = data.filter((item, index) => {

  const last = data[index - 1]

  if (typeof last == 'undefined') return true

  return item.endTime > last.endTime

})


const greaterThanAll = data.reduce((result, current) => {

  const endTimeGreaterThanRest = result.every((item) => current.endTime > item.endTime)

  if (endTimeGreaterThanRest) result.push(current)

  return result

}, [])


console.log(greaterThanLast)

console.log(greaterThanAll)


查看完整回答
反对 回复 2021-10-29
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

假设您不想更改原始数组并假设您想删除“最低”值,您可以执行单个 for 循环:

  • 跟踪以前的值。

  • 将前一个值与当前值进行比较。

  • 根据您的需要过滤该值。

关于代码的注释直接在下面,这个解决方案意味着只循环一次原始数组,利用单个 for-of 循环。

const input = [

  { 

    title: 'Title 1',

    startTime: '2019-09-26T06:00:00+0100',

    endTime: '2019-09-26T08:30:00+0100' 

  },

  { 

    title: 'Title 2',

    startTime: '2019-09-26T08:00:00+0100',

    endTime: '2019-09-26T08:15:00+0100' 

  },

  { 

    title: 'Title 3',

    startTime: '2019-09-26T08:30:00+0100',

    endTime: '2019-09-26T09:25:00+0100' 

  },

  { 

    title: 'Title 4',

    startTime: '2019-09-26T09:25:00+0100',

    endTime: '2019-09-26T10:25:00+0100' 

  },

  { 

    title: 'Title 5',

    startTime: '2019-09-26T10:25:00+0100',

    endTime: '2019-09-26T11:00:00+0100' 

  }

];


function fixChronologicalItems(arr) {

  // Keep track of the previous item.

  let res = [], previous;

  // Iterate all the items of the array.

  for (let i = 0; i < arr.length; i++) {

    // assume the current item is the looped one.

    let item = arr[i];

    // if our accumulator is not empty, acquire its last element considering it the previous item.

    if (res[res.length - 1]) previous = res[res.length - 1];

    else previous = arr[i], item = arr[i+1], i++; // if it doesn't, consider the current item the previous one, and the current item the next one, so increase the index by one to properly skip the next item.

    // Acquire both datetimes.

    let [previousDate, nextDate] = [new Date(previous.endTime), new Date(item.endTime)];

    // if the previous item's date is before the next one, both items should be kept.

    if (previousDate < nextDate) {

      res.push(item); // <-- this will become the next "previous".

    }

    else res.push(previous); // <-- Otherwise, only the greatest date (which is the previous one) should be kept.

  }

  // finally, return the accumulator.

  return res;

}

const res = fixChronologicalItems(input);

console.log(res);


查看完整回答
反对 回复 2021-10-29
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

James Coyle 的回答比我的要流畅得多,但这是我对事情的看法。


在我的解决方案中,我检查下一个值是否小于当前值,如果是,则使用delete从数组中删除它。


如果我们得到数组中的最后一个值,那么我们就不再需要检查下一项,因此我们不进入 if 语句并中断循环。


var dataset = [{

    title: 'Title 1',

    startTime: '2019-09-26T06:00:00+0100',

    endTime: '2019-09-26T08:30:00+0100'

  },

  {

    title: 'Title 2',

    startTime: '2019-09-26T08:00:00+0100',

    endTime: '2019-09-26T08:15:00+0100'

  },

  {

    title: 'Title 3',

    startTime: '2019-09-26T08:30:00+0100',

    endTime: '2019-09-26T09:25:00+0100'

  },

  {

    title: 'Title 4',

    startTime: '2019-09-26T09:25:00+0100',

    endTime: '2019-09-26T10:25:00+0100'

  },

  {

    title: 'Title 5',

    startTime: '2019-09-26T10:25:00+0100',

    endTime: '2019-09-26T11:00:00+0100'

  }

]


var lengthOfArray = dataset.length;

for (i = 0; i < dataset.length; i++) {

  if (lengthOfArray !== i + 1) {

    var dt = new Date(dataset[i].endTime),

    dt2 = new Date(dataset[i + 1].endTime);

    if (dt2 < dt) delete dataset[i + 1].endTime;

  }

  else {

    break;

  }

}

console.log(dataset);


查看完整回答
反对 回复 2021-10-29
  • 3 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号