【问题标题】:Convert one list into another [closed]将一个列表转换为另一个[关闭]
【发布时间】:2020-03-15 14:50:32
【问题描述】:

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

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

输入是这样的:

[
  {
    "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]
            }
          }
        }
      ]
    }
  ]
}

【问题讨论】:

  • 到目前为止,您尝试解决什么问题?可以分享一下代码吗?
  • 请访问help center查看内容和How to Ask。提示:发布努力和代码。
  • 所以...按.information.label分组并连接所有.list.lists?
  • 搜索 javascript reduce 组

标签: javascript algorithm ecmascript-6


【解决方案1】:

首先,但不要指望我会为你做所有的工作

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) )

【讨论】:

    【解决方案2】:

    诀窍是在迭代列表时跟踪索引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))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多