基于2个值的多维数组分组

时间:2019-10-28 11:10:45

标签: javascript arrays loops group-by

我在徘徊,如果你们中的任何一个可以帮助我。

我在每行(Order,Material,StorageBin,Batch,Greasing(yes,no),Qty)... 800行中都有一个值数组

我已经完成了groupBy函数...它返回所有具有总计QTY的值...

问题是我需要groupBY组件进行润滑,所以我想添加一个GROUP NUMBER ... Component1可以与Component3,4,5一起作为GROUP1,然后Component2可以与Component6,7,8 AS GROUP2一起...

一个组件具有许多订购号,但也可以连接更多组件进行润滑...

基本上是:({if greasing = yes check if component is in group...if no create group and add component...but all that based on ORDER NUMBER so I can tell why do they connect

我头疼...有人吗?!

数据

从:

[4363348,comp1,C01020001,1,130,greasing],
[4363348,comp2,C02060201,1,130,greasing],
[4363348,comp3,C01040001,1,130,No],
[4363349,comp1,C01020001,1,130,greasing],
[4363349,comp4,C02060201,1,130,greasing],
[4363349,comp5,C01040001,1,130,No],
[4363350,comp6,C01020001,1,130,greasing],
[4363350,comp7,C02060201,1,130,greasing],
[4363350,comp5,C01040001,1,130,No]

收件人:

[4363348,comp1,C01020001,1,130,greasing,1],
[4363348,comp2,C02060201,1,130,greasing,1],
[4363348,comp3,C01040001,1,130,No,0],
[4363349,comp1,C01020001,1,130,greasing,1],
[4363349,comp4,C02060201,1,130,greasing,1],
[4363349,comp5,C01040001,1,130,No,0],
[4363350,comp6,C01020001,1,130,greasing,2],
[4363350,comp7,C02060201,1,130,greasing,2],
[4363350,comp5,C01040001,1,130,No,0]

1 个答案:

答案 0 :(得分:0)

您可以采用 Material 列并从字符串中获取muber,并取整数第三个值并加一个。

const getMaterialNumber = s => 1 + Math.floor(s.match(/\d+$/) / 3);

var array = [[4363348, 'comp1', 'C01020001', 1, 130, 'greasing'], [4363348, 'comp2', 'C02060201', 1, 130, 'greasing'], [4363348, 'comp3', 'C01040001', 1, 130, 'No'], [4363349, 'comp1', 'C01020001', 1, 130, 'greasing'], [4363349, 'comp4', 'C02060201', 1, 130, 'greasing'], [4363349, 'comp5', 'C01040001', 1, 130, 'No'], [4363350, 'comp6', 'C01020001', 1, 130, 'greasing'], [4363350, 'comp7', 'C02060201', 1, 130, 'greasing'], [4363350, 'comp5', 'C01040001', 1, 130, 'No']],
    result = array.map(a => [...a, a[5] === 'greasing' ? getMaterialNumber(a[1]) : 0]);

result.forEach(a => console.log(...a));
.as-console-wrapper { max-height: 100% !important; top: 0; }