在对象中展平数组

时间:2016-11-22 16:02:18

标签: arrays json jq flatten

是否有一种优雅的方法可以为每个数组项复制一个对象?

#include <iostream>
struct A{};
struct B{};
struct C{};
struct D{};

template<typename T> constexpr const char* GetNameOfList();
//here you may want to make it return nullptr by default

template<>constexpr const char* GetNameOfList<A>(){return "A";}
template<>constexpr const char* GetNameOfList<B>(){return "B";}
template<>constexpr const char* GetNameOfList<C>(){return "C";}

int main(){
   std::cout << GetNameOfList<A>() << '\n';
   std::cout << GetNameOfList<B>() << '\n';
   std::cout << GetNameOfList<C>() << '\n';
   //std::cout << GetNameOfList<D>() << '\n'; //compile error here
}

1 个答案:

答案 0 :(得分:1)

这是一种扩展任意JSON对象的所有数组值键的编程方法:

def blowup(a): 
  def b(f): {(f): (.[f] | if type == "array" then .[] else . end)};

  if a|length == 0 then {} else b(a[0]) + blowup(a[1:]) end;

blowup(keys)

示例:

输入:{a:0,b:[1,2],c:[3,4]}

输出:

{"a":0,"b":1,"c":3}
{"a":0,"b":2,"c":3}
{"a":0,"b":1,"c":4}
{"a":0,"b":2,"c":4}
相关问题