久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

按數組中的多個屬性對對象進行分組,然后將它

Group objects by multiple properties in array then sum up their values(按數組中的多個屬性對對象進行分組,然后將它們的值相加)
本文介紹了按數組中的多個屬性對對象進行分組,然后將它們的值相加的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

按多個屬性對數組中的元素進行分組是與我的問題最匹配,因為它確實通過數組中的多個鍵對對象進行分組.問題是這個解決方案沒有總結屬性值然后刪除重復項,而是將所有重復項嵌套在一個二維數組中.

Grouping elements in array by multiple properties is the closest match to my question as it indeed groups objects by multiple keys in an array. Problem is this solution doesn't sum up the properties value then remove the duplicates, it instead nests all the duplicates in a two-dimensional arrays.

預期行為

我有一組對象,必須按 shapecolor 分組.

I have an array of objects which must be grouped by shape and 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 值,然后刪除重復項.

Objects in this array are considered duplicates only if both their shape and color are the same. If they are, I want to respectively sum up their used and instances values then delete the duplicates.

所以在這個例子中,結果數組可能只包含四種組合:square redsquare bluecircle redcircle blue

So in this example result array may only contain four combinations : square red, square blue, circle red, circle blue

問題

我在這里嘗試了一種更簡單的方法:

I tried a simpler approach here:

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}]

如何讓我的函數按形狀和顏色正確分組對象?即總結它們的值并刪除重復項?

How can I get my function to properly group the objects by shape and color ? i.e. sum up their values and remove the duplicates ?

推薦答案

使用 Array#reduce 帶有一個輔助對象來對相似的對象進行分組.對于每個對象,檢查組合的 shapecolor 是否存在于幫助程序中.如果沒有,請使用 添加到幫助程序Object#assign 創建對象的副本,并推送到數組.如果是,請將其值添加到 usedinstances.

Use Array#reduce with a helper object to group similar objects. For each object, check if the combined shape and color exists in the helper. If it doesn't, add to the helper using Object#assign to create a copy of the object, and push to the array. If it does, add it's values to used and instances.

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,則使用 Map 收集值,然后通過 傳播 地圖#values:

If you can use ES6, you use a Map to collect the values, and then convert it back to an array by spreading the Map#values:

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

這篇關于按數組中的多個屬性對對象進行分組,然后將它們的值相加的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 精品一区二区三区四区五区 | 黄色精品 | 久久国产福利 | 久久久久久久久久久91 | 亚洲最大av | 精品视频一区二区三区四区 | 啪视频在线| 北条麻妃视频在线观看 | 国产一区二区三区免费观看在线 | 久久国产亚洲 | 欧美精品a∨在线观看不卡 国产精品久久国产精品 | 久久久高清 | 91精品久久久 | 99精品国自产在线 | av在线一区二区三区 | 99视频在线免费观看 | 日韩视频1| 91视频导航 | 欧美日韩亚洲国产 | 精品久久久久久亚洲综合网 | 精品国产一区二区三区久久 | 久草免费在线视频 | 国产一区二区三区四区三区四 | 久久久精 | 91极品欧美视频 | 国产亚洲精品综合一区 | 国产一区www | 黄a在线观看 | 久久国产精品免费一区二区三区 | 久久久资源 | 精品欧美一区二区在线观看欧美熟 | 日韩欧美二区 | 影音先锋中文字幕在线观看 | 精品一区二区久久久久久久网站 | 美女国产 | 久久精品久久久久久 | 国产精品精品3d动漫 | 中文在线一区二区 | 久久久久国产精品午夜一区 | 欧美性网 | 欧美午夜精品理论片a级按摩 |