比较一个对象数组

时间:2018-02-15 16:04:08

标签: javascript arrays object underscore.js

服务器给我下一个数组:

let reportsInDB = [
  {comment:"asdasd", date:"13-02-2018", issueId:"1005"},
  {comment:"asdasd", date:"14-02-2018", issueId:"1005"},
  {comment:"asdasd", date:"15-02-2018", issueId:"1005"},
  {comment:"qwe", date:"13-02-2018", issueId:"1006"},
  {comment:"asd123123asd", date:"14-02-2018", issueId:"1006"},
  {comment:"asd123123asd", date:"15-02-2018", issueId:"1006"},
  {comment:"lalala", date:"15-02-2018", issueId:"1007"},
]

有没有办法让下一个:

比较具有今天日期的对象是否与“昨天”对象具有相同的注释(当然是该对象的issueId)并将其推送(可能不是将整个对象,只是它的issId)推送到新数组,arrRisk = [] for例如,但如果评论连续3天相同 - 例如推送到另一个数组arrIncident = []。

所以最后的输出应该是下一个:

arrRisk = ["1006"]
arrIncident = ["1005]

带有issueId 1007的对象不应该转到任何新数组,因为昨天或前天对其issId没有相同的评论。

我也得到了服务变量,这应该有助于比较:

today = "15-02-2018"
yesterday = "14-02-2018"
beforeYesterday = "13-02-2018"

我正在尝试像

这样的东西
reportsInDB.map(x => {
    reportsInDB.map(r =>{
    if( x.date === today && 
        r.date === yesterday && 
        x.issueId === r.issueId && 
        x.comment === r.comment
    ){
        reportsToRisk.push(r)
    } 
  })
})

但是输出完全不是我需要的(例如我不需要发行100I5推送到RiskArray)并且双三重周期并不像我所知的那样好......

这就是我在jsfiddle

中尝试的内容

希望我的解释最终结果应该是明确的。

来自服务器的排序方式: enter image description here

2 个答案:

答案 0 :(得分:2)

您需要注意另一个问题的相同评论不会发挥作用。这是一个解决方案,首先按问题对数据进行分组,然后收集每个问题的注释。然后它很简单。这也不依赖于某种排序顺序:

const reportsToRisk = [];
const reportsToIncident = [];
const tdy = moment().format('DD-MM-YYYY');
const yst = moment().subtract(1, 'day').format('DD-MM-YYYY');
const dby = moment().subtract(2, 'day').format('DD-MM-YYYY');
const reportsInDB = [
    {comment:"asdasd", date: dby, issueId:"1005"},
    {comment:"asdasd", date: yst, issueId:"1005"},
    {comment:"asdasd", date: tdy, issueId:"1005"},
    {comment:"qwe", date: dby, issueId:"1006"},
    {comment:"asd123123asd", date: yst, issueId:"1006"},
    {comment:"asd123123asd", date: tdy, issueId:"1006"},
    {comment:"123d", date: tdy, issueId:"1007"},
];

// create a map keyed by issueId:
const map = new Map(reportsInDB.map( report => [report.issueId, {}] ));
// Fill the date / comment pairs
for (const report of reportsInDB) {
    map.set(report.issueId, Object.assign(map.get(report.issueId), 
            { [report.date]: report.comment }));
}
for (const [issueId, comments] of map.entries()) {
    if (comments[tdy] === comments[yst]) {
        if (comments[tdy] === comments[dby]) {
            reportsToIncident.push(issueId);
        } else {
            reportsToRisk.push(issueId);
        }
    }
}
//console.log("reportsInDB", reportsInDB)
console.log('reportsToRisk', reportsToRisk)
console.log('reportsToIncident', reportsToIncident)        
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>

答案 1 :(得分:1)

使用.reduce重复比较之前的条目。请注意,我使用reportsInDB.slice(1)作为我.reduce的数组。这使得变量respostInDB[idx]始终指向数组中的上一个条目。

之后,运行循环以检查该新数组中的重复。重复意味着您连续发生了2起事件。

之后,我必须.filter删除arrRiskarrIncident中也存在的ID。

let reportsInDB = [
  {comment:"asdassd", date:"13-02-2018", issueId:"1005"},
  {comment:"asdasd", date:"14-02-2018", issueId:"1005"},
  {comment:"asdasd", date:"15-02-2018", issueId:"1005"},
  {comment:"qwe", date:"13-02-2018", issueId:"1006"},
  {comment:"asd123123asd", date:"14-02-2018", issueId:"1006"},
  {comment:"asd123123asd", date:"15-02-2018", issueId:"1006"},
  {comment:"lalala", date:"14-02-2018", issueId:"1007"},
  {comment:"lalala", date:"15-02-2018", issueId:"1007"},
  {comment:"lalala", date:"15-02-2018", issueId:"1007"}
]
const matches = reportsInDB.slice(1).reduce((acc, obj, idx) => {
  if (reportsInDB[idx].comment === obj.comment) {
    return acc.concat([obj.issueId])
  } else {
    return acc
  }
}, [])

let arrRisk = []
let arrIncident = []
for (let i = 0; i < matches.length; i++) {
  if (matches[i + 1] === matches[i]) {
    arrIncident.push(matches[i])
  } else {
    arrRisk.push(matches[i])
  }
}
arrRisk = arrRisk.filter((id) => !arrIncident.includes(id))
const issues = {arrRisk, arrIncident}
console.log(issues)