当前位置: 首页 > 面试题库 >

通过数组中的多个属性对对象进行分组,然后对其值求和

司马辉
2023-03-14
问题内容

通过多个属性对数组中的元素进行分组最符合我的问题,因为它确实通过数组中的多个键对对象进行了分组。问题是此解决方案无法汇总属性值,然后删除重复项,而是将所有重复项嵌套在二维数组中。

预期行为

我有一个对象数组,必须按shape和进行分组color

var arr = [
    {shape: 'square', color: 'red', used: 1, instances: 1},
    {shape: 'square', color: 'red', used: 2, instances: 1},
    {shape: 'circle', color: 'blue', used: 0, instances: 0},
    {shape: 'square', color: 'blue', used: 4, instances: 4},
    {shape: 'circle', color: 'red', used: 1, instances: 1},
    {shape: 'circle', color: 'red', used: 1, instances: 0},
    {shape: 'square', color: 'blue', used: 4, instances: 5},
    {shape: 'square', color: 'red', used: 2, instances: 1}
];

这个数组中的对象被视为重复仅当其shapecolor是相同的。如果它们是,我想分别总结它们的usedinstances值,然后删除重复项。

因此,在这个例子的结果阵列可以仅含有四种组合:square redsquare bluecircle redcircle blue

问题

我在这里尝试了一种更简单的方法:

var arr = [

    {shape: 'square', color: 'red', used: 1, instances: 1},

    {shape: 'square', color: 'red', used: 2, instances: 1},

    {shape: 'circle', color: 'blue', used: 0, instances: 0},

    {shape: 'square', color: 'blue', used: 4, instances: 4},

    {shape: 'circle', color: 'red', used: 1, instances: 1},

    {shape: 'circle', color: 'red', used: 1, instances: 0},

    {shape: 'square', color: 'red', used: 4, instances: 4},

    {shape: 'square', color: 'red', used: 2, instances: 2}

];



result = [];



arr.forEach(function (a) {

    if ( !this[a.color] && !this[a.shape] ) {

        this[a.color] = { color: a.color, shape: a.shape, used: 0, instances: 0 };

        result.push(this[a.color]);

    }

    this[a.color].used += a.used;

    this[a.color].instances += a.instances;

}, Object.create(null));



console.log(result);

但它输出

[{shape: "square", color: "red", used: 11, instances: 9},
{shape: "circle", color: "blue", used: 4, instances: 4}]

而不是预期的结果:

[{shape: "square", color: "red", used: 5, instances: 3},
{shape: "circle", color: "red", used: 2, instances: 1},
{shape: "square", color: "blue", used: 11, instances: 9},
{shape: "circle", color: "blue", used: 0, instances: 0}]

如何获得按形状和颜色正确分组对象的功能?即总结他们的价值,并删除重复的?


问题答案:

将Array#reduce与帮助器对象一起使用可以对相似的对象进行分组。对于每个对象,检查合并的shape和是否color存在于帮助器中。如果不是,请使用Object#assign添加到帮助器中以创建对象的副本,然后将其压入数组。如果是这样,请将其值添加到usedinstances

var arr = [{"shape":"square","color":"red","used":1,"instances":1},{"shape":"square","color":"red","used":2,"instances":1},{"shape":"circle","color":"blue","used":0,"instances":0},{"shape":"square","color":"blue","used":4,"instances":4},{"shape":"circle","color":"red","used":1,"instances":1},{"shape":"circle","color":"red","used":1,"instances":0},{"shape":"square","color":"blue","used":4,"instances":5},{"shape":"square","color":"red","used":2,"instances":1}];



var helper = {};

var result = arr.reduce(function(r, o) {

  var key = o.shape + '-' + o.color;



  if(!helper[key]) {

    helper[key] = Object.assign({}, o); // create a copy of o

    r.push(helper[key]);

  } else {

    helper[key].used += o.used;

    helper[key].instances += o.instances;

  }



  return r;

}, []);



console.log(result);

如果你可以使用ES6,您可以使用地图收集的值,然后将其转换回一个数组蔓延的地图#值:

const arr = [{"shape":"square","color":"red","used":1,"instances":1},{"shape":"square","color":"red","used":2,"instances":1},{"shape":"circle","color":"blue","used":0,"instances":0},{"shape":"square","color":"blue","used":4,"instances":4},{"shape":"circle","color":"red","used":1,"instances":1},{"shape":"circle","color":"red","used":1,"instances":0},{"shape":"square","color":"blue","used":4,"instances":5},{"shape":"square","color":"red","used":2,"instances":1}];



const result = [...arr.reduce((r, o) => {

  const key = o.shape + '-' + o.color;



  const item = r.get(key) || Object.assign({}, o, {

    used: 0,

    instances: 0

  });



  item.used += o.used;

  item.instances += o.instances;



  return r.set(key, item);

}, new Map).values()];



console.log(result);


 类似资料:
  • 按多个属性对数组中的元素进行分组最符合我的问题,因为它确实是按数组中的多个键对对象进行分组的。问题是,这种解决方案不求和属性值,然后删除重复项,而是将所有重复项嵌套在二维数组中。 预期行为 我有一个对象数组,必须按和分组。 此数组中的对象只有在它们的和相同时才被认为是重复的。如果是,我想分别总结它们的和值,然后删除重复项。 因此,在这个示例中,结果数组可能只包含四种组合:,,, 问题 我在这里尝试

  • 问题内容: 是否可以通过多个值过滤对象数组? 例如,在下面的示例中,我可以按term_ids 5和6进行过滤,然后同时键入car吗? 如果可以轻松使用,肯定可以使用库。 问题答案: 您可以使用Array.filter

  • 这是我的输入,我有一个数组,其中名称键在多个对象中具有值“Foo1”,因此我希望在单独的数组中相对于“name”键值和其他信息的唯一对象。 下面的数组是唯一的名称键和其他值和状态键从所有匹配的对象存储在new_obj键。

  • 问题内容: 有谁知道(如果可能的话,也用破折号)通过对象键对对象数组进行分组然后根据分组创建新的对象数组的方法吗?例如,我有一系列汽车对象: 我想制作一组新的汽车对象,并按以下类别分组: 问题答案: 。简单,并允许在分组结构中的对象中进行一些重复。 但是,OP还要求删除重复的密钥。如果您想一路走下去: 产量: 如果您想使用Underscore.js进行此操作,请注意,其版本称为。

  • 我已经设法编写了一个使用Java8Streams API的解决方案,该解决方案首先按对象路由的值对其列表进行分组,然后对每组中的对象数进行计数。它返回映射路由->long。代码如下: 和路由类: 应转换为: 请注意,映射的键数为2条路由,它是lastUpdated值最大的一条。

  • 问题内容: 如何通过匹配对象属性从数组中删除对象? 请只使用本机JavaScript。 我在使用接头时遇到麻烦,因为每次删除的长度都会减少。使用克隆并在原始索引上进行拼接仍然会给您带来长度减少的问题。 问题答案: 我以为你用过这样的东西? 修复bug所需要做的就是在下一次减少,然后(也可以选择向后循环): 为了避免线性时间删除,可以编写要 保留 在数组上的数组元素: 为了避免在现代运行时中进行线性