将一个列表转换为另一个列表

时间:2020-03-15 14:50:32

标签: javascript algorithm ecmascript-6

我的大脑在跳动。我需要创建一个将接受输入并将其转换为另一种格式的函数。

它应该从对象数组创建一个信息对象,该对象具有按标签排序的数组,并且内部列表值也应具有特殊顺序。我需要找到修复它的方法或算法。

输入内容如下:

[
  {
    "information": [
      {
        "label": "Wich­tigs­te Leis­tun­gen",
        "list": [
          {
            "list": {
              "1953-24uxno": {
                "rows": [1]
              }
            }
          },
          {
            "list": {
              "1953-24uxno": {
                "rows": [2]
              }
            }
          }
        ]
      },
      {
        "label": "Allgemein",
        "list": [
          {
            "list": {
              "1953-24uxno": {
                "rows": [11]
              }
            }
          },
          {
            "list": {
              "1953-24uxno": {
                "rows": [12]
              }
            }
          }
        ]
      }
    ]
  },
  {
    "information": [
      {
        "label": "Wich­tigs­te Leis­tun­gen",
        "list": [
          {
            "list": {
              "1953-obbpw8": {
                "rows": [3]
              }
            }
          },
          {
            "list": {
              "1953-obbpw8": {
                "rows": [4]
              }
            }
          }
        ]
      },
      {
        "label": "Allgemein",
        "list": [
          {
            "list": {
              "1953-obbpw8": {
                "rows": [31]
              }
            }
          },
          {
            "list": {
              "1953-obbpw8": {
                "rows": [32]
              }
            }
          }
        ]
      }
    ]
  }
]

输出:

{
  "information": [
    {
      "label": "Wich­tigs­te Leis­tun­gen",
      "list": [
        {
          "list": {
            "1953-24uxno": {
              "rows": [1]
            },
            "1953-obbpw8": {
              "rows": [3]
            }
          }
        },
        {
          "list": {
            "1953-24uxno": {
              "rows": [2]
            },
            "1953-obbpw8": {
              "rows": [4]
            }
          }
        }
      ]
    },
    {
      "label": "Allgemein",
      "list": [
        {
          "list": {
            "1953-24uxno": {
              "rows": [11]
            },
            "1953-obbpw8": {
              "rows": [31]
            }
          }
        },
        {
          "list": {
            "1953-24uxno": {
              "rows": [12]
            },
            "1953-obbpw8": {
              "rows": [32]
            }
          }
        }
      ]
    }
  ]
}

2 个答案:

答案 0 :(得分:2)

首先,但不要指望我会为您完成所有工作

const truc1 = 
  [ { "information": 
      [ { "label": "Wich­tigs­te Leis­tun­gen"
        , "list": 
          [ { "list": { "1953-24uxno": { "rows": [ 1 ] } } } 
          , { "list": { "1953-24uxno": { "rows": [ 2 ] } } } 
          ] 
        } 
      , { "label": "Allgemein"
        , "list": 
          [ { "list": { "1953-24uxno": { "rows": [ 11 ] } } } 
          , { "list": { "1953-24uxno": { "rows": [ 12 ] } } } 
    ] } ] } 
  , { "information": 
      [ { "label": "Wich­tigs­te Leis­tun­gen"
        , "list": 
          [ { "list": { "1953-obbpw8": { "rows": [ 3 ] } } } 
          , { "list": { "1953-obbpw8": { "rows": [ 4 ] } } } 
          ] 
        } 
      , { "label": "Allgemein"
        , "list": 
          [ { "list": { "1953-obbpw8": { "rows": [ 31 ] } } } 
          , { "list": { "1953-obbpw8": { "rows": [ 32 ] } } } 
  ] } ] } ]


const truc2 = truc1.reduce((a,e)=>
  {
  let k = Object.keys(e)[0]
  if (!a[k]) a[k] = []
  // continue with e[k]... ( is eq to "information": [...
  return a
  },{})


console.log( JSON.stringify(truc2,0,2) )

答案 1 :(得分:1)

诀窍是在迭代列表(在i之后)时跟踪索引label,以便知道在输出中将列表分配给哪个索引

const data = [{"information":[{"label":"Wich­tigs­te Leis­tun­gen","list":[{"list":{"1953-24uxno":{"rows":[1]}}},{"list":{"1953-24uxno":{"rows":[2]}}}]},{"label":"Allgemein","list":[{"list":{"1953-24uxno":{"rows":[11]}}},{"list":{"1953-24uxno":{"rows":[12]}}}]}]},{"information":[{"label":"Wich­tigs­te Leis­tun­gen","list":[{"list":{"1953-obbpw8":{"rows":[3]}}},{"list":{"1953-obbpw8":{"rows":[4]}}}]},{"label":"Allgemein","list":[{"list":{"1953-obbpw8":{"rows":[31]}}},{"list":{"1953-obbpw8":{"rows":[32]}}}]}]}]
const out = {}
data.forEach(({ information }) => {
  information.forEach(({ label, list }) => {
    out[label] = out[label] || { list: [] }
    const row = out[label]
    // [{list:{19...}}, {list:{19...}}]
    list.forEach(({ list }, i) => {
      const [k, v] = Object.entries(list)[0]
      row.list[i] = row.list[i] || { list: {} }
      row.list[i].list[k]  = v
    })
  })
})
console.log(JSON.stringify({ information: out }, null, 2))