将数组分组为一个

时间:2019-08-31 15:41:35

标签: javascript arrays loops split append

有一个数组,其中包含appointmentID(第一个值)和supperBillID(第二个值),以逗号分隔。 AppointmentID将是唯一的,但是superBillID只能在连续位置相同。我想要的是一个包含所有appointmentID值的数组,这些值具有相同的billingID,用逗号分隔。

我写了下面的代码,但没有得到正确的输出:

var fg = ['10000021,23', '10000022,23', '10000023,24', '10000024,25', '10000025,25', '10000026,25', '10000027,26', '10000028,27'];
var tab = [];
var gbl = 0;

for (var i = 0; i < fg.length; i++, gbl++) {
    var vb = fg[gbl].split(',')[1]; // Will use try catch here
    var mainAr = fg[gbl].split(',')[0];

    for (var j = i + 1; j < fg.length; j++) {
        if (vb == fg[j].split(',')[1]) {
            mainAr = mainAr + ',' + fg[j].split(',')[0];
            gbl++;
        }
        else {
            break;
        }
        tab.push(mainAr, vb);
    }
}

样本输入:

var input = ['10000021,23', '10000022,23', '10000023,24', '10000024,25', '10000025,25', '10000026,25', '10000027,26', '10000028,27'];

预期输出:

output = ['10000021,10000023',23]
         ['10000023',24]
         ['10000024,10000025,10000026',25]
         ['10000027',26]
         ['10000028',27]

3 个答案:

答案 0 :(得分:0)

您可以reduce个数组,每个supperBillID作为累加器中的键。如果supperBillID已经存在,请更新0索引。否则,将密钥添加到累加器并将其设置为数组。使用Object.values()

以数组形式获取值

var fg = ['10000021,23', '10000022,23', '10000023,24', '10000024,25', '10000025,25', '10000026,25', '10000027,26', '10000028,27'];

const merged = fg.reduce((acc, o) => {
  const [appId, billId] = o.split(',');
  if (acc[billId])
    acc[billId][0] += `,${appId}`;
  else
    acc[billId] = [appId, billId]
  return acc;
}, {})

const output = Object.values(merged);

console.log(output)

答案 1 :(得分:0)

第一步,您可以使用appointmentdID作为Array.reduce()来将appointmentID的值分组为一个Set(以避免与billingID重复的值)。分组的关键。

然后,您可以将先前生成的对象的Array.map() entries复制为最终所需的结构。在这里,我假设您想要两个可能的输出之一:您最后显示的 A)数组数组,或 B)的字符串数组,其中输入的样式。

var input = ['10000021,23', '10000022,23', '10000023,24', '10000024,25', '10000025,25', '10000026,25', '10000027,26', '10000028,27'];

let output = input.reduce((acc, str) =>
{
    const [appointmentID, billingID] = str.split(",");
    acc[billingID] = acc[billingID] || new Set();
    acc[billingID].add(appointmentID);
    return acc;
}, {});

// Map to array of arrays:
let out1 = Object.entries(output).map(([k, v]) => [[...v].join(","), +k]);
console.log("Array of arrays", out1);

// Map to array of strings:
let out2 = Object.entries(output).map(([k, v]) => [...v, k].join(","));
console.log("Array of strings", out2);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

基于以下假设的另一种方法,但不是通用的:“ AppointmentID将是唯一的,但superBillID只能在连续的位置上是相同的 < / em>”可能是:

var input = ['10000021,23', '10000022,23', '10000023,24', '10000024,25', '10000025,25', '10000026,25', '10000027,26', '10000028,27'];
let output = [];

for (let i = 0; i < input.length; i++)
{
    const [appointmentID, billingID] = input[i].split(",");
    const len = output.length - 1;

    if (output[len] && output[len][1] === +billingID)
        output[len][0] += "," + appointmentID;
    else
        output.push([appointmentID, +billingID]);
}

console.log(output);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

答案 2 :(得分:0)

首先将数组转换为方便的对象数组,然后根据所述条件缩小数组

const input = [
  "10000021,23",
  "10000022,23",
  "10000023,24",
  "10000024,25",
  "10000025,25",
  "10000026,25",
  "10000027,26",
  "10000028,27"
];

const dataset = input.map(item => {
  const nodes = item.split(",");

  return {
    appointmentid: nodes[0],
    superbillid: nodes[1]
  };
});

const output = dataset.reduce((accumulator, current) => {
  const item = accumulator.find(
    element => element[element.length - 1] === current.superbillid
  );

  if (item !== undefined) {
    item.unshift(current.appointmentid);
  } else {
    accumulator.push([current.appointmentid, current.superbillid]);
  }

  return accumulator;
}, []);

console.log(output);

要获取字符串数组,您可以尝试像这样在每个数组项上使用map

const recipient = output.map(item => item.join(',')));

这将返回

[
  "10000022,10000021,23",
  "10000023,24",
  "10000026,10000025,10000024,25",
  "10000027,26",
  "10000028,27"
]