I want to return a key in the object, how to do it?
json1
{
"statisticsOption": [
{
"key": "date",
"prop": "date",
"name": "日期",
"show": true,
"description": "日期"
},
{
"key": "failedSubscriptionUser",
"prop": "failedSubscriptionUser",
"name": "当日失败订阅用户数",
"show": true,
"description": "不重复的用户数"
},
{
"key": "successSubscription",
"prop": "successSubscription",
"name": "当日成功订阅",
"show": true,
"description": "不含退款"
}
]
}
json2
{
"statisticsOption": [
{
"key": "date",
"prop": "date",
"name": "日期1",
"show": true,
"description": "日期1"
},
{
"key": "failedSubscriptionUser",
"prop": "failedSubscriptionUser",
"name": "当日失败订阅用1户数",
"show": true,
"description": "不重复的1用户数"
},
{
"key": "successSubscription",
"prop": "successSubscription",
"name": "当日成功1订阅",
"show": true,
"description": "不含1退款"
}
]
}
Each object in the array has a unique value: key
let diffpatcher = jsondiffpatch.create({
objectHash: (obj) => {
return obj.key
}
})
console.log(diffpatcher .diff(json1, json2))
I want to return the only key value followed. However, the key value has not actually been modified. My data can not be changed to an object. What should I do?
if I understand correctly this is doable writing a jsondiffpatch plugin (see README), but can you share an expected result (how would the output json look) to better understand the idea?
let json1 = {
statisticsOption: [
{
'key': 'date',
'prop': 'date',
'name': '日期',
'show': true,
'description': '日期'
},
{
'key': 'failedSubscriptionUser',
'prop': 'failedSubscriptionUser',
'name': '当日失败订阅用户数',
'show': true,
'description': '不重复的用户数'
},
{
'key': 'successSubscription',
'prop': 'successSubscription',
'name': '当日成功订阅',
'show': true,
'description': '不含退款'
}
]
}
let json2 = {
statisticsOption: [
{
'key': 'date',
'prop': 'date',
'name': '日期',
'show': true,
'description': '日期'
},
{
'key': 'date',
'prop': 'date',
'name': '日期',
'show': true,
'description': '日期1'
},
{
'key': 'failedSubscriptionUser',
'prop': 'failedSubscriptionUser',
'name': '当日失败1订阅用户数',
'show': true,
'description': '不重1复的用户数'
},
{
'key': 'successSubscription',
'prop': 'successSubscription',
'name': '当日成功订阅',
'show': true,
'description': '不含1退款'
}
]
}
let diffpatcher = jsondiffpatch.create({
objectHash: (obj) => {
// console.log(obj)
return obj.key
}
})
console.log(diffpatcher.diff(json1, json2))
this console is return

- Whether added or deleted or modified, there is no identifier to identify it.
- I do not know how to configure the option to return the key value together in the object.
- The array key it returns is not what I want
I do not know what to do.
it seems that the solution shown at https://github.com/benjamine/jsondiffpatch/issues/190#issuecomment-316469152 would work for you?
Please forgive me for the way he wrote
context.setResult({
__id: context.right.id,
__model: context.left.constructor.name
})
There is a setResult here does not appear in the README
@benjamine
[{
"key": "date",
"prop": "date",
"name": "日期",
"show": true,
"description": "日期"
}
]
Can return the currently modified object
it's not directly in the README, but there is an example in the Plugins page linked there: https://github.com/benjamine/jsondiffpatch/blob/master/docs/plugins.md#plugin-example
but surely there could be more extended docs on authoring them, basicallly setResult sets the output for a specific node in the diff, that way you can use it to include things like an extra property (like key) or any other details.