比较一个对象数组中的对象
Compare objects in one array of objects
服务器给我下一个数组:
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"},
]
有没有办法制作下一个:
比较今天日期的对象是否与 "yesterday" 对象具有相同的注释(当然该对象的 issueId 相同)并将其推送(可能不是推送整个对象,只是推送 issueId)到新数组,arrRisk = [] 例如,但如果评论连续 3 天相同 - 推送到另一个数组,例如 arrIncident = []。
因此最终输出应该如下所示:
arrRisk = ["1006"]
arrIncident = ["1005]
issueId 为 1007 的对象不应该进入任何新数组,因为昨天或前天没有与其 issueId 相同的评论。
我还得到了服务变量,这应该有助于比较:
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)
}
})
})
但输出完全不是我需要的(例如,我不需要将 issueId 1005 推送到 riskArray)并且双三循环并不像我所知的那么好...
这就是我在 jsfiddle
中尝试的
希望我对最终结果的解释是清楚的。
来自服务器的排序方式:
使用.reduce
与上一个条目重复比较。请注意,我使用 reportsInDB.slice(1)
作为我的数组 .reduce
。这使得变量 respostInDB[idx]
始终指向数组中的前一个条目。
之后,运行 循环检查新数组中的重复项。重复意味着您连续发生了 2 次事件。
之后,我必须 .filter
删除 arrRisk
中也存在于 arrIncident
中的 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)
您需要注意 另一个 问题的相同评论不会起作用。这是一个解决方案,首先按问题对数据进行分组,然后收集每个问题的评论。然后就很简单了。这也不依赖于某种排序顺序:
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>
服务器给我下一个数组:
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"},
]
有没有办法制作下一个:
比较今天日期的对象是否与 "yesterday" 对象具有相同的注释(当然该对象的 issueId 相同)并将其推送(可能不是推送整个对象,只是推送 issueId)到新数组,arrRisk = [] 例如,但如果评论连续 3 天相同 - 推送到另一个数组,例如 arrIncident = []。
因此最终输出应该如下所示:
arrRisk = ["1006"]
arrIncident = ["1005]
issueId 为 1007 的对象不应该进入任何新数组,因为昨天或前天没有与其 issueId 相同的评论。
我还得到了服务变量,这应该有助于比较:
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)
}
})
})
但输出完全不是我需要的(例如,我不需要将 issueId 1005 推送到 riskArray)并且双三循环并不像我所知的那么好...
这就是我在 jsfiddle
中尝试的希望我对最终结果的解释是清楚的。
来自服务器的排序方式:
使用.reduce
与上一个条目重复比较。请注意,我使用 reportsInDB.slice(1)
作为我的数组 .reduce
。这使得变量 respostInDB[idx]
始终指向数组中的前一个条目。
之后,运行 循环检查新数组中的重复项。重复意味着您连续发生了 2 次事件。
之后,我必须 .filter
删除 arrRisk
中也存在于 arrIncident
中的 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)
您需要注意 另一个 问题的相同评论不会起作用。这是一个解决方案,首先按问题对数据进行分组,然后收集每个问题的评论。然后就很简单了。这也不依赖于某种排序顺序:
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>