- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有2个对象,一个来自SQL数据库,另一个来自JSON REST API。在使用来自api的新数据更新数据库中的行之前,我不仅要检查ID是否已在数据库中出现(这意味着要进行更新而不是插入操作),还要检查属性的大部分。原因是,当它确实在数据库端更新时,会添加一个附加的“lastUpdate”日期时间以供以后在PowerBI中进行处理,如果我仅检查ID,则每次从API中输入条目时都会触发“lastUpdate”即使它的属性没有实际更新,也已经存在于数据库中。两侧的单个对象(它们以阵列形式出现)如下:
案例和注释:
重要:本部分是根据Nina Scholz要求的(现在)接受的解决方案添加的。阅读时请记住这一点。
callerLocation.id
,但是,如果未设置callerLocation,则父callerLocation
已经是null
,因此使得id
无法访问null
。例如,然后我可以将callerLocationID: null
与callerLocation.id
进行比较,因为后者是null
,并且null
与null
true
)还是“否,它们是不相同的”(
false
)。如果不需要知道它们是否不相同,则将整个对象发送给更新。
null
,则数据库一侧的路径基本上是固定的,因为这是因为允许在数据库中使用ID
结尾的属性(incidentID
除外)均引用API中的单个嵌套id
值。例如callerID
引用caller.id
,callerBranchID
引用callerBranch.id
和operatorID
引用operator.id
impact
引用impact.name
urgency
引用urgency.name
priority
引用priority.name
duration
引用duration.name
escalationOperator
引用escalationOperator.id
optionalField
东西都可以忽略// Database Object
{
incidentID: '0dc1a10f-2899-485a-b814-f72f29c9a15a',
status: 'secondLine',
briefDescription: 'Support niet bereikbaar',
callDate: '2018-04-10T19:01:00.000Z',
lastUpdate: '2018-04-18T14:02:17.000Z',
number: 'M1804 021',
request: '10-04-2018 21:02 Middelkoop, Paul: \nIk kan de servicedesk niet bereiken, telkens in gesprek',
callerID: '4e723042-0037-4e05-a362-e65c620ba734',
callerBranchID: 'f66e7804-b57a-4418-a991-997e574ead29',
callerLocationID: null,
externalNumber: null,
categoryID: 'cafb0af8-e43a-4391-ac9e-a0345abbcc4f',
subcategoryID: 'f56099a9-7c60-45e3-94b4-6555a79d4bd7',
callTypeID: '04b678a6-791e-4662-9bc8-97573555f15e',
entryTypeID: 'a9c486fd-a93e-565e-bfeb-17619fafe1a8',
branchID: null,
locationID: null,
impact: null,
urgency: null,
priority: null,
duration: null,
operatorID: 'a17aba85-13a7-4ac6-8c57-693a512b633e',
operatorGroupID: 'a17aba85-13a7-4ac6-8c57-693a512b633e',
supplierID: null,
targetDate: '2019-04-10T15:30:00.000Z',
onHold: false,
onHoldDate: null,
onHoldDuration: 0,
feedbackMessage: null,
feedbackRating: null,
processingStatus: 'Afgemeld',
completed: true,
completedDate: '2018-04-10T19:09:00.000Z',
closed: true,
closedDate: null,
closureCode: null,
creatorID: '226082ea-8d74-4dee-ae1e-74c33c883792',
creationDate: '2018-04-10T19:02:34.000Z',
timeSpent: 0,
timeSpentFirstLine: 0,
timeSpentSecondLineAndPartials: 0,
costs: 0,
escalationStatus: null,
escalationReason: null,
escalationOperator: null,
modifier: '226082ea-8d74-4dee-ae1e-74c33c883792',
modificationDate: '2018-04-10T19:12:08.000Z',
expectedTimeSpent: 0,
majorCall: false,
majorCallID: null,
publishToSSD: false,
monitored: false,
archivingReason: null
}
// API Object
{
id: '0dc1a10f-2899-485a-b814-f72f29c9a15a',
status: 'secondLine',
number: 'M1804 021',
request: '10-04-2018 21:02 Middelkoop, Paul: \nIk kan de servicedesk niet bereiken, telkens in gesprek',
requests: '/tas/api/incidents/id/0dc1a10f-2899-485a-b814-f72f29c9a15a/requests',
action: '/tas/api/incidents/id/0dc1a10f-2899-485a-b814-f72f29c9a15a/actions',
attachments: '/tas/api/incidents/id/0dc1a10f-2899-485a-b814-f72f29c9a15a/attachments',
caller: {
id: '4e723042-0037-4e05-a362-e65c620ba734',
dynamicName: 'Mafficioli del Castelletto, Richard',
branch: {
id: 'f66e7804-b57a-4418-a991-997e574ead29',
name: 'Ask Roger! Delft',
clientReferenceNumber: '',
timeZone: 'Europe/Amsterdam',
extraA: null,
extraB: null
}
},
callerBranch: {
id: 'f66e7804-b57a-4418-a991-997e574ead29',
name: 'Ask Roger! Delft',
clientReferenceNumber: '',
timeZone: 'Europe/Amsterdam',
extraA: null,
extraB: null
},
callerLocation: null,
branchExtraFieldA: null,
branchExtraFieldB: null,
briefDescription: 'Support niet bereikbaar',
externalNumber: '',
category: {
id: 'cafb0af8-e43a-4391-ac9e-a0345abbcc4f',
name: 'Communicatie'
},
subcategory: {
id: 'f56099a9-7c60-45e3-94b4-6555a79d4bd7',
name: 'Vaste telefonie'
},
callType: {
id: '04b678a6-791e-4662-9bc8-97573555f15e',
name: 'Klacht'
},
entryType: {
id: 'a9c486fd-a93e-565e-bfeb-17619fafe1a8',
name: 'Mondeling'
},
object: null,
branch: null,
location: null,
impact: null,
urgency: null,
priority: null,
duration: null,
targetDate: '2019-04-10T15:30:00.000+0000',
onHold: false,
onHoldDate: null,
onHoldDuration: 0,
feedbackMessage: null,
feedbackRating: null,
operator: {
id: 'a17aba85-13a7-4ac6-8c57-693a512b633e',
status: 'operatorGroup',
name: 'Systeembeheer'
},
operatorGroup: {
id: 'a17aba85-13a7-4ac6-8c57-693a512b633e',
name: 'Systeembeheer'
},
supplier: null,
processingStatus: {
id: '70b2967d-e248-4ff9-a632-ec044410d5a6',
name: 'Afgemeld'
},
completed: true,
completedDate: '2018-04-10T19:09:00.000+0000',
closed: true,
closedDate: '2018-04-10T19:12:00.000+0000',
closureCode: null,
timeSpent: 0,
timeSpentFirstLine: 0,
timeSpentSecondLineAndPartials: 0,
costs: 0,
escalationStatus: null,
escalationReason: null,
escalationOperator: null,
callDate: '2018-04-10T19:01:00.000+0000',
creator: {
id: '226082ea-8d74-4dee-ae1e-74c33c883792',
name: 'Middelkoop, Paul'
},
creationDate: '2018-04-10T19:02:34.000+0000',
modifier: {
id: '226082ea-8d74-4dee-ae1e-74c33c883792',
name: 'Middelkoop, Paul'
},
modificationDate: '2018-04-10T19:12:08.000+0000',
majorCall: false,
majorCallObject: null,
publishToSsd: false,
monitored: false,
expectedTimeSpent: 0,
archivingReason: null,
optionalFields1: {
boolean1: false,
boolean2: false,
boolean3: false,
boolean4: false,
boolean5: false,
number1: 0,
number2: 0,
number3: 0,
number4: 0,
number5: 0,
date1: null,
date2: null,
date3: null,
date4: null,
date5: null,
text1: '',
text2: '',
text3: '',
text4: '',
text5: '',
memo1: null,
memo2: null,
memo3: null,
memo4: null,
memo5: null,
searchlist1: null,
searchlist2: null,
searchlist3: null,
searchlist4: null,
searchlist5: null
},
optionalFields2: {
boolean1: false,
boolean2: false,
boolean3: false,
boolean4: false,
boolean5: false,
number1: 0,
number2: 0,
number3: 0,
number4: 0,
number5: 0,
date1: null,
date2: null,
date3: null,
date4: null,
date5: null,
text1: '',
text2: '',
text3: '',
text4: '',
text5: '',
memo1: null,
memo2: null,
memo3: null,
memo4: null,
memo5: null,
searchlist1: null,
searchlist2: null,
searchlist3: null,
searchlist4: null,
searchlist5: null
}
}
// tdIncidents is the array of objects from the API
// dbIncidents is the array of objects from the database
// tdinci is my iterator, consider it the "i" in the for loop
// at this point it has already been confirmed that both dbIncidents and tdIncidents have at least 1 entry thus using [0] won't give any problems
const db = _.first(fuse.search(tdIncidents[tdinci].id)),
td = tdIncidents[tdinci],
dbKeys = _.keys(dbIncidents[0]),
tdKeys = _.keys(tdIncidents[0]),
identicalKeysTd = _.pick(td, (value, key) => dbKeys.includes(key)),
identicalKeysDb = _.pick(db, (value, key) => tdKeys.includes(key));
{ status: 'secondLine',
number: 'M1804 021',
request: '10-04-2018 21:02 Middelkoop, Paul: \nIk kan de servicedesk niet bereiken, telkens in gesprek',
briefDescription: 'Support niet bereikbaar',
externalNumber: '',
impact: null,
urgency: null,
priority: null,
duration: null,
targetDate: '2019-04-10T15:30:00.000+0000',
onHold: false,
onHoldDate: null,
onHoldDuration: 0,
feedbackMessage: null,
feedbackRating: null,
processingStatus: { id: '70b2967d-e248-4ff9-a632-ec044410d5a6', name: 'Afgemeld' },
completed: true,
completedDate: '2018-04-10T19:09:00.000+0000',
closed: true,
closedDate: '2018-04-10T19:12:00.000+0000',
closureCode: null,
timeSpent: 0,
timeSpentFirstLine: 0,
timeSpentSecondLineAndPartials: 0,
costs: 0,
escalationStatus: null,
escalationReason: null,
escalationOperator: null,
callDate: '2018-04-10T19:01:00.000+0000',
creationDate: '2018-04-10T19:02:34.000+0000',
modifier:
{ id: '226082ea-8d74-4dee-ae1e-74c33c883792',
name: 'Middelkoop, Paul' },
modificationDate: '2018-04-10T19:12:08.000+0000',
majorCall: false,
monitored: false,
expectedTimeSpent: 0,
archivingReason: null }
{ status: 'secondLine',
briefDescription: 'Support niet bereikbaar',
callDate: '2018-04-10T19:01:00.000Z',
number: 'M1804 021',
request: '10-04-2018 21:02 Middelkoop, Paul: \nIk kan de servicedesk niet bereiken, telkens in gesprek',
externalNumber: null,
impact: null,
urgency: null,
priority: null,
duration: null,
targetDate: '2019-04-10T15:30:00.000Z',
onHold: false,
onHoldDate: null,
onHoldDuration: 0,
feedbackMessage: null,
feedbackRating: null,
processingStatus: 'Afgemeld',
completed: true,
completedDate: '2018-04-10T19:09:00.000Z',
closed: true,
closedDate: null,
closureCode: null,
creationDate: '2018-04-10T19:02:34.000Z',
timeSpent: 0,
timeSpentFirstLine: 0,
timeSpentSecondLineAndPartials: 0,
costs: 0,
escalationStatus: null,
escalationReason: null,
escalationOperator: null,
modifier: '226082ea-8d74-4dee-ae1e-74c33c883792',
modificationDate: '2018-04-10T19:12:08.000Z',
expectedTimeSpent: 0,
majorCall: false,
monitored: false,
archivingReason: null }
identicalKeys
(.isEqual
)检查这两个_.isEqual(identicalKeysDb, identicalKeysTd)
对象的相等性,但无济于事。除了我直接在数据库中存储一些键而没有附加“ID”(可以在数据库端固定)这一事实之外,更紧迫的问题是数据库将为诸如null
之类的值提供externalNumber
,但API会给出''
。 if ()
来检查每个属性是否与对应属性相对应。我不介意需要一些节点程序包来简化此比较,因此,如果绝对是解决方案,请也共享它。
existingIncidents
的数组,该数组随后与任何新事件一起返回。发生这种情况如下:
async filterIncidents() {
const dbIncidents = await this.getDbIncidents(this.lastFetchTimestamp),
fuseOpts = {
'shouldSort': true,
'findAllMatches': true,
'threshold': 0,
'location': 0,
'distance': 100,
'maxPatternLength': 36,
'minMatchCharLength': 36,
'keys': ['incidentID']
},
fuse = new Fuse(dbIncidents, fuseOpts),
tdIncidents = await this.getTdIncidents(this.lastFetchTimestamp);
const existIncidents = [],
newIncidents = [];
if (!dbIncidents.length) {
for (const tdinci in tdIncidents) {
newIncidents.push(tdIncidents[tdinci]);
}
} else {
for (const tdinci in tdIncidents) {
if (fuse.search(tdIncidents[tdinci].id).length) {
// The value checking magic I need has to happen here. Some pseudo code:
// if (values are different) {
existIncidents.push(tdIncidents[tdinci]);
// } else {
// do nothing
// }
} else {
newIncidents.push(tdIncidents[tdinci]);
}
}
}
return {
'new': newIncidents,
'existing': existIncidents
};
}
最佳答案
这是一种通过将数组用于不同样式的对象以及指向每个对象的属性的相对路径的方法。
关键功能是单个函数getValue
,它接受一个对象和一个通向所需属性的关键数组,并返回找到的值或该链的最后找到的值。
另一个函数迭代给定的关系对象,并(实际上)显示两个值,以进行补偿和稍后的操作,例如更新或其他需要的操作。
function getValue(object, keys) {
return keys.reduce((o, k) => o && typeof o === 'object' ? o[k] : o, object);
}
function compaire(objects, relations) {
relations.forEach(relation => {
var values = relation.map((keys, i) => getValue(objects[i], keys));
console.log(...values);
});
}
var objectA = { foo: { bar: 42 }, a: { b: { c: 'baz' } }, callerLocation: null },
objectB = { fooBar: 42, nested: { abc: 'bau' }, callerLocationID: null },
objects = [objectA, objectB],
relations = [
[['foo', 'bar'], ['fooBar']],
[['a', 'b', 'c'], ['nested', 'abc']],
[['callerLocation', 'id'], ['callerLocationID']],
[['x', 'u'], ['x', 'y']]
];
compaire(objects, relations);
关于javascript - 如何有效地比较具有不同键但可能匹配值的2个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49901436/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!