logo
Tags down

shadow

Grouping array into one


By : gerfen
Date : May 04 2020, 08:20 AM

There one array that contains appointmentID (first value) and supperBillID (second value) separated by a comma. The AppointmentID will be unique but superBillID can be the same in consecutive position only. What I want is one array that contains all the appointmentID values, which have same billingID, separated by comma.

I write below code but I'm not getting the proper output:

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

Sample Input:

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

Expected Output:

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

to fix this , On  first step you can use Array.reduce() to group the appointmentdID values into a Set (to avoid duplicates values for appointmentID) using billingID as the key to group.

Then, you can Array.map() the entries of the previously generated object to the final desired structure. Here, I'm going to assume you want one of two possible outputs: A) an array of arrays as you showed last, or B) an array of strings with the style of the input.

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

Another alternative, not so generic, based on the assumption: "The AppointmentID will be unique but superBillID can be the same in consecutive position only" could be:

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



Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org