【问题标题】:How to convert values of array to array filtered如何将数组的值转换为过滤后的数组
【发布时间】:2019-11-23 13:41:38
【问题描述】:

我有这个问题,我用的是Angular 8,希望你能帮帮我,我已经很久没能解决了。

我有这个数组:

datos = [{
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '1'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '2'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '3'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '1'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '2'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '4'
  },
]

我需要像这样过滤和分组它们:

result = [
  {ADMON: {0: 1, 
           1: 2, 
           2: 3, 
           3: 4}},
  {USER:  {0: 1, 
           1: 2, 
           2: 4}}
];

我试过很多方法都做不到,希望你能帮助我,在此先谢谢你了。

【问题讨论】:

  • 我认为结果可以是 result = [ {ADMON: {0: 1, 1: 2, 2: 3, {USER: {0: 1, 1: 2, 2: 4}} ]; 因为 ADMIN 没有值为 4 "3: 4" ,请检查一下。
  • 你能展示一下你的尝试——你最好的尝试吗?
  • 非常感谢大家,非常感谢。

标签: arrays angular typescript angular7 angular8


【解决方案1】:

这是一种潜在的解决方案,它使用多种数组方法来完成手头的任务。

const datos = [{
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '1'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '2'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'ADMON',
    Value: '3'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '1'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '2'
  },
  {
    Description: 'Puede Insertar',
    Id: 1,
    Type: 'USER',
    Value: '4'
  }
];

const grouped = datos.reduce((acc, el) => {
  if (!acc[el.Type]) acc[el.Type] = {};
  const index = Object.values(acc[el.Type]).length;
  acc[el.Type] = {
    ...acc[el.Type],
    [index]: el.Value
  }
  return acc;
}, {});

const final = Object.entries(grouped).map(([key, val]) => {
  return { [key]: val }
});

console.log(final);

【讨论】:

    【解决方案2】:

    const datos = [{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'ADMON',Value:'3'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'1'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'2'},{Description:'Puede Insertar',Id:1,Type:'USER',Value:'4'}];
    
    
    let result = Object.entries(datos.reduce((acc, {Type, Value}) => {
    	         (acc[Type] = acc[Type] || []).push(Value);
    	           return acc;
                   }, {}))
                 .map((o) => { return {[o[0]]: {...o[1]}}});
    	
    console.log(result);

    【讨论】:

    • 我没做,给不了你积分,我还没积分。我非常感谢你。
    【解决方案3】:

    尝试使用这样的东西:

    前段时间发现了这个功能,用了很多次:

    let groupBy = function (xs, key) {
     return xs.reduce(function (rv, x) {
        (rv[x[key]] = rv[x[key]] || []).push(x);
        return rv;
     }, {});
    };
    

    如果你在你的场景中尝试:

        let groupBy = function (xs, key) {
         return xs.reduce(function (rv, x) {
            (rv[x[key]] = rv[x[key]] || []).push(x);
            return rv;
         }, {});
        };
    
    
        let datos = [{
          Description: 'Puede Insertar',
          Id: 1,
          Type: 'ADMON',
          Value: '1'
        },
          {
            Description: 'Puede Insertar',
            Id: 1,
            Type: 'ADMON',
            Value: '2'
          },
          {
            Description: 'Puede Insertar',
            Id: 1,
            Type: 'ADMON',
            Value: '3'
          },
          {
            Description: 'Puede Insertar',
            Id: 1,
            Type: 'USER',
            Value: '1'
          },
          {
            Description: 'Puede Insertar',
            Id: 1,
            Type: 'USER',
            Value: '2'
          },
          {
            Description: 'Puede Insertar',
            Id: 1,
            Type: 'USER',
            Value: '4'
          },
        ]
        
        console.log(groupBy(datos, 'Type'));

    【讨论】:

    • 非常感谢,它对我有用,我还有很多东西要学习,不便之处敬请见谅
    【解决方案4】:

    我可能会这样做:

    // First pass reduce to a map of your index / value maps keyed by type.
    // Grouping tends to be easier in a map than an array.
    const groupByType = datos.reduce((result, d, i) => {
      result[d.Type] = result[d.Type] || {};
      result[d.Type][i] = d.Value;
      return result;
    }, {} as Record<string, Record<string, string>>);
    
    // Once we have a map, we can convert it to the final array
    const arrayResult = Object.keys(groupByType).map(t => {
      return {
        [t]: groupByType[t]
      };
    });
    

    值得一提的是,我故意没有在 reduce 回调中使用扩展语法。在许多情况下,这样做可能很好,但如果您的数据集足够大,可能会对性能产生影响,因为您将在每次迭代中创建 + 复制。由于创建对象和设置其属性完全封装在 reduce 函数中,因此与其他可能需要考虑不变性的情况相比,不应该担心变异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多