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

166
Views
Remove multiple elements from nested arrays

Assume there is a nested array like this:

[
    [ 'one', 'third ],
    [ 'one', 'second', 'fourth' ],
    [ 'one', 'third' ],
]

I need to make the values unique by order priority: If an element is existing in the first array, it should be removed from the second and third. An element of the second array should not exist in the third.

So the result should be:

[
    [ 'one', 'third ],
    [ 'second', 'fourth' ],
    [],
]

I would iterate over each array and each element, but this removes an element only from the next array (which is missing the last array or errors if the loop is at the last array) and it feels very hacky...

for (let i = 0; i < array.length; i++) {
    const element = array[i];
    for (let j = 0; j < element.length; j++) {
        const string = element[j];
        const index = array[i + 1].indexOf(string)
        if (index !== -1) {
            array[i + 1].splice(index, 1)
        }
    }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could take a Set and filter the values with a lookup and adding the value, if not seen.

const
    values = new Set,
    data = [['one', 'third'], ['one', 'second', 'fourth'], ['one', 'third']],
    result = data.map(a => a.filter(v => !values.has(v) && values.add(v)));

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

about 4 years ago · Juan Pablo Isaza Report

0

This can be done via a two step process:

  1. Use Set on each sub-array to remove any duplicate elements
  2. Use .filter() and .some() methods to iterate through each element of each sub-array and leave out (filter) any that are in any lower-index sub-array. The first sub-array - index 0 - is returned whole as there are no lower-index sub-arrays to compare with.

const data = [[ 'one', 'third'],[ 'one','second','fourth'], ['one', 'third']];
const result = data.map(arr => [...new Set(arr)]).map(
    (arr,i,a) => i === 0 ? arr : arr.filter(
        v => !a.slice(0,i).some(x => x.includes(v))
    )
);
console.log( result );

about 4 years ago · Juan Pablo Isaza Report

0

An immutable approach, using reduce() and flat():

const data = [[ 'one', 'third' ], [ 'one', 'second', 'fourth' ],[ 'one', 'third' ]];
const result = data.reduce((acc, val) => [
     ...acc,
     val.filter(item => !acc.flat().includes(item))
], []);

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!