Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

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

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

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

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs