给定一个输入数组,该数组包含包含各种关键点的对象和active_candidates_group
个关键点,其中该关键点是候选对象的数组,创建一个新的输出数组,其中每个候选对象与来自父对象的关键点相结合.
请务必注意以下几点:
- 父对象中有数百个关键点,对它们进行硬编码并不理想.
- 父对象的关键点的顺序并不重要.
- 父对象和候选对象之间不应该有重复的关键点.
输入
[
{
"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 }
}
]
}
]
期望输出
[
{
"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 }
}
]
到目前为止,我try 的是:
[
{
"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": ""
}
}
}
}
]