Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Lodash,如何将这些数据转换为所需的格式?_Javascript_Lodash - Fatal编程技术网

Javascript 使用Lodash,如何将这些数据转换为所需的格式?

Javascript 使用Lodash,如何将这些数据转换为所需的格式?,javascript,lodash,Javascript,Lodash,我对lodash相当陌生,我怀疑它可以帮助我以我想要的方式转换数据。我一直在读这本书,但说实话,它并没有深入,我没有看到我需要的正确的操作组合 以下是我想做的: var configs = [ { configId: 1, options: [ {categoryId: 1, id: 100}, {categoryId: 2, id: 200}, {categoryId: 3, id: 300} ] }

我对lodash相当陌生,我怀疑它可以帮助我以我想要的方式转换数据。我一直在读这本书,但说实话,它并没有深入,我没有看到我需要的正确的操作组合

以下是我想做的:

var configs = [ { 
      configId: 1,
      options: [ {categoryId: 1, id: 100},
                 {categoryId: 2, id: 200},
                 {categoryId: 3, id: 300} ] },
    { 
      configId: 2,
      options: [ {categoryId: 1, id: 105},
                 {categoryId: 2, id: 210} ] },
    { 
      configId: 3,
      options: [ {categoryId: 2, id: 200},
                 {categoryId: 1, id: 165},
                 {categoryId: 3, id: 300} ] }
];

//  I want the above to look like this:
/*
[ {categoryId: 1, ids: [100, 105, 165]},
  {categoryId: 2, ids: [200, 210]},
  {categoryId: 3, ids: [300]} ]
*/
如果你想做实验,这里有一个例子

当我看到这个问题时,我想:

  • 将所有
    配置
    对象组合在一起,这样我就有了一个大型的
    选项数组
    对象
  • 分组依据
    categoryId
  • 然后我真的迷路了。。。我想我可以在这里使用reduce(),但我还认为我需要一个map()来实际生成转换的对象
  • 不管怎样,我一直在重复这个问题,没有取得进展,我希望有人能给我指出正确的方向。

    使用ES2015语法:

    _.map(
      _.groupBy(_.flatMap(configs, config => config.options), 'categoryId'), 
      (val, key) => ({categoryId: key, ids: _.uniq(_.map(val, v => v.id)) })
    )
    
    ES5兼容(由Babel生成):

    说明:

    _.flatMap(configs, config => config.options)
    
    从每个配置对象中获取
    选项
    数组,将它们展平为
    [{categoryId:xx,id:yy},…]的单个数组

    [ {categoryId:1,id:100},
      {categoryId:2,id:200},
      {categoryId:3,id:300},
      {categoryId:1,id:105},
      {categoryId:2,id:210},
      {categoryId:2,id:200},
      {categoryId:1,id:165},
      {categoryId:3,id:300} ]
    
    
    _.groupBy(..., 'categoryId')
    
    {
        1:[
            {categoryId:1,id:100},
            {categoryId:1,id:105},
            {categoryId:1,id:165} ],
        2:[
            {categoryId:2,id:210},
            {categoryId:2,id:200},
            {categoryId:2,id:200} ],
        3:[
            {categoryId:3,id:300},
            {categoryId:3,id:300} ] }
    
    _.map(..., (val, key) => ({categoryId: key, ids: _.uniq(_.map(val, v => v.id)) }))
    
    按其
    类别id在数组上方分组,如
    [{xx:[categoryId:xx,id:yy},…]

    [ {categoryId:1,id:100},
      {categoryId:2,id:200},
      {categoryId:3,id:300},
      {categoryId:1,id:105},
      {categoryId:2,id:210},
      {categoryId:2,id:200},
      {categoryId:1,id:165},
      {categoryId:3,id:300} ]
    
    
    _.groupBy(..., 'categoryId')
    
    {
        1:[
            {categoryId:1,id:100},
            {categoryId:1,id:105},
            {categoryId:1,id:165} ],
        2:[
            {categoryId:2,id:210},
            {categoryId:2,id:200},
            {categoryId:2,id:200} ],
        3:[
            {categoryId:3,id:300},
            {categoryId:3,id:300} ] }
    
    _.map(..., (val, key) => ({categoryId: key, ids: _.uniq(_.map(val, v => v.id)) }))
    
    接收
    val=[{xx:[categoryId:xx,id:yy},…]
    key:xx
    并将它们映射到对象,其中
    categoryId
    设置为接收的键,
    ids
    映射到来自分组对象的唯一
    id
    的数组。结果是您想要的数组

    [ {
        categoryId:1,
        ids:[100,105,165]},
      {
        categoryId:2,
        ids:[200,210]},
      {
        categoryId:3,
        ids:[300]}]
    
    var配置=[{
    配置ID:1,
    选项:[
    {类别id:1,id:100},
    {类别id:2,id:200},
    {类别id:3,id:300}
    ]
    }, { 
    配置ID:2,
    选项:[
    {类别id:1,id:105},
    {类别id:2,id:210}
    ]
    }, { 
    配置ID:3,
    选项:[
    {类别id:2,id:200},
    {类别id:1,id:165},
    {类别id:3,id:300}
    ]
    }];
    var结果=u.chain(配置)
    .flatMap(config=>config.options)
    .groupBy(id=>id.categoryId)
    .map((列表,类别ID)=>({
    类别ID:+类别ID,
    ID:u.chain(列表)
    .map(x=>x.id)
    .uniq()
    .value()
    }))
    .value();
    document.body.textContent=JSON.stringify(结果,null,2);
    正文{
    空白:预包装;
    }

    谢谢你的回答,虽然两个答案都显示了相同的技巧,但我选择了另一个,因为我喜欢解释部分。非常好,我非常感谢你回答的解释部分,谢谢。我编辑并添加了每个阶段的输出,因为我的大脑是这样思考的(有限?),也许其他人也会觉得它很有用。感谢@SteveK的编辑,查看实际数据可能比我的“模板数据”更有用。