jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

I want to return a key in the object, how to do it?

Open vxhly opened this issue 8 years ago • 6 comments

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?

vxhly avatar Nov 07 '17 16:11 vxhly

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?

benjamine avatar Nov 08 '17 01:11 benjamine

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 image

  1. Whether added or deleted or modified, there is no identifier to identify it.
  2. I do not know how to configure the option to return the key value together in the object.
  3. The array key it returns is not what I want image

I do not know what to do.

vxhly avatar Nov 08 '17 01:11 vxhly

it seems that the solution shown at https://github.com/benjamine/jsondiffpatch/issues/190#issuecomment-316469152 would work for you?

benjamine avatar Nov 08 '17 01:11 benjamine

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

vxhly avatar Nov 08 '17 02:11 vxhly

@benjamine

[{
      "key": "date",
      "prop": "date",
      "name": "日期",
      "show": true,
      "description": "日期"
    }
]

Can return the currently modified object

vxhly avatar Nov 08 '17 06:11 vxhly

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.

benjamine avatar Nov 08 '17 17:11 benjamine