如何将数组的值转换为过滤的数组

时间:2019-11-23 02:25:42

标签: arrays angular typescript angular7 angular8

我遇到了这个问题,我使用的是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}}
];

我已经尝试了很多方法,但是我不能,希望您能帮助我,非常感谢。

4 个答案:

答案 0 :(得分:0)

尝试使用类似这样的内容:

前一段时间,我发现了此功能并使用了很多时间:

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

答案 1 :(得分:0)

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

<hr class="hrNav">
<nav class="navbar">
  <ul class="mainNav">
    <li class="navitem"><a href="#about">About me</a></li>
    <li class="navitem"><a href="#contact">Contact</a></li>
    <li class="navitem"><input type="text" placeholder="Search..."></li>
  </ul>
</nav>

答案 2 :(得分:0)

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 :(得分:0)

这是我可能会做的:

// 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函数中,因此与其他可能需要考虑不可变性的情况相比,无需担心任何有关突变的问题。

相关问题