Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
nested unique flat in javascript

Is there some super new ES JavaScript magic that can achieve this ? (I can write a function to achieve this, was just wondering if any new ES202* technique exists )

let arr = [
    ['cb', ''],
    ['cb', '34'],
    ['cb', '35'],
    ['rb', '1']
];

/*
Required Output : 
[['cb', ['34', '35']], ['rb', '1']]
*/

console.log([...new Set(arr.flat(1))])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could take the upcoming Array#groupBy.

Approach with (own) polyfill.

Array.prototype.groupBy ??= function (callbackfn, thisArg) {
    const O = Object(this);
    const len = O.length >>> 0;
    if (typeof callbackfn !== 'function') throw new TypeError(callbackfn + ' is not a function');

    let k = 0;
    const groups = {};

    while (k < len) {
        const Pk = Number(k).toString();
        const kValue = O[Pk];
        const propertyKey = callbackfn.call(thisArg, kValue, Number(k), O);
        (groups[propertyKey] ??= []).push(kValue);
        ++k;
    }
    return groups;
};


const
    array = [['cb', ''], ['cb', '34'], ['cb', '35'], ['rb', '1']],
    result = Object
        .entries(array
            .filter(([, v]) => v)
            .groupBy(([k]) => k)
        )
        .map(([k, v]) => [k, v.map(([, v]) => v)]);

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!