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

245
Views
How can I extract from an array of arrays all the arrays that have the same value in their first field?

The following function elegantly finds duplicates in 1-dimensional arrays:


    const findDuplicates  = (dataArray) => {
      const duplicates = dataArray.filter((e, index, arr) => arr.indexOf(e) !== index);
      return (duplicates);
    };

When I send it (for example) this array ['123456', '787877', '763223', '787877', '854544'] it returns ['787877'].

What I need is something similar that works for a 2-d array so (for instance) inputting


    [ 
      ['123456', 'Smith'],
      ['787877',  'Jones'],
      ['763223', 'Waldo'],
      ['787877',  'Quagmire'],
      ['854544',  'Miller']
    ]

returns

[['787877', 'Jones'], ['787877', 'Quagmire']]

(To be clear, I'm only interested in whether the 1st field of each sub-array is a dupe.)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could take an object and use a boolean values to indicae duplicates. Then filter the array.

const
    findDuplicates = data => {
        const
            keys = data.reduce((r, [v]) => {
                r[v] = r[v] !== undefined;
                return r;
            }, {});

        return data.filter(([v]) => keys[v]);
    },
    data = [['123456', 'Smith'], ['787877', 'Jones'], ['763223', 'Waldo'], ['787877', 'Quagmire'], ['854544', 'Miller']],
    result = findDuplicates(data);

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

about 4 years ago · Juan Pablo Isaza Report

0

const findDuplicates = (dataArray) => {
  const duplicates = dataArray.filter((e, index, arr) => {
    return arr.some((val, i) => (index !== i && val[0] === e[0]))
  })
  return (duplicates);
};

const result = findDuplicates([
  ['123456', 'Smith'],
  ['787877', 'Jones'],
  ['763223', 'Waldo'],
  ['787877', 'Quagmire'],
  ['854544', 'Miller']
])

console.log(result)

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!