英文:
Jolt spec to apply all keys in a parent object to all objects in an array
问题
Here is the translation of the code part you provided:
给定一个包含各种键的输入数组和一个 active_candidates_group 键,该键是候选对象的数组,创建一个新的输出数组,其中每个候选对象与父对象的键组合在一起。
需要注意以下几点:
- 父对象中有数百个键,最好不要硬编码它们。
 - 父对象的键的顺序不重要。
 - 父对象和候选对象之间不应有重复的键。
 
输入
[
  {
    "id": 123,
    "name": "foo",
    // { 数百个其他键 }
    "active_candidates_group": [
      {
        "person_id": 123,
        "person_name": "john"
        // { 与‘候选’相关的许多字段 ——不希望与父对象重复 }
      },
      {
        "person_id": 321,
        "person_name": "bob"
        // { 与‘候选’相关的许多字段 ——不希望与父对象重复 }
      }
    ]
  }
]
期望的输出
[
   {
      "id": 123,
      "name": "foo",
      // { 来自父对象的数百个其他键 }
      "person_id": 123,
      "person_name": "john"
      // { 其他候选字段 }
   },
   {
      "id": 123,
      "name": "foo",
      // { 来自父对象的数百个其他键 }
      "person_id": 321,
      "person_name": "bob"
      // { 其他候选字段 }
   }
]
我已翻译了代码部分,如果需要进一步解释或帮助,请随时告诉我。
英文:
Given an input array with objects containing various keys and an active_candidates_group key, with this key being an array of candidate objects, create a new output array where each candidate object is combined with the keys from the parent object.
It is important to note the following points:
- There are hundreds of keys in the parent object and it's not ideal to hard-code them.
 - The parent object's keys' order does not matter.
 - There should be no duplicate keys between the parent object and the candidate objects.
 
Input
[
  {
    "id": 123,
    "name": "foo",
    // { hundreds of other keys }
    "active_candidates_group": [
      {
        "person_id": 123,
        "person_name": "john"
        // { many fields specific to ‘candidate’ —no duplicates to parent expected }
      },
      {
        "person_id": 321,
        "person_name": "bob"
        // { many fields specific to ‘candidate’ —no duplicates to parent expected }
      }
    ]
  }
]
Desired Output
[
   {
      "id": 123,
      "name": "foo",
      // { hundreds of other keys from parent }
      "person_id": 123,
      "person_name": "john"
      // { other candidate fields }
   },
   {
      "id": 123,
      "name": "foo",
      // { hundreds of other keys from parent }
      "person_id": 321,
      "person_name": "bob"
      // { other candidate fields }
   }
]
What I tried so far:
[
  {
    "operation": "shift",
    "spec": {
      "*": { // Iterate for each object in the input array
        "active_candidates_group": { // Match 'active_candidates_group' key
          "*": { // Iterate for each object inside 'active_candidates_group'
            "@2": "&2[&1]", // Copy all keys from the parent object to the same index in the output array
            "*": "&2[&1].&" // Copy the current object to its corresponding index in the output array
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "active_candidates_group": {
        "*": {
          "active_candidates_group": ""
        }
      }
    }
  }
]
答案1
得分: 2
你离解决方案很近了,只需在两个不同级别使用"*": "[&1].&"稍微扭曲一下,而不需要硬编码单独的属性,如下所示:
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "active_candidates_group": {
          "*": {
            "@2": { "*": "[&1].&" }, // 在更深的级别匹配
            "*": "[&1].&"
          }
        }
      }
    }
  },
  { // 仅对数组进行清除处理
    "operation": "remove",
    "spec": {
      "*": {
        "active_candidates_group": ""
      }
    }
  }
]
在网站http://jolt-demo.appspot.com/上的演示如下:
英文:
You are so close to the solution, just distort a bit through use of "*": "[&1].&" at two different level without hardcoding the attributes individually such as
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "active_candidates_group": {
          "*": {
            "@2": { "*": "[&1].&" }, // match at the deeper level
            "*": "[&1].&"
          }
        }
      }
    }
  },
  { // only purify for the array everyehere  
    "operation": "remove",
    "spec": {
      "*": {
        "active_candidates_group": ""
      }
    }
  }
]
the demo on the site http://jolt-demo.appspot.com/ is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论