Convert array of arrays to array without duplicates I have the following array, and I want to convert it to an array without duplicates.


const a = [
    {
        items: [
            { outerKey: { innerKey: 1 } },
            { outerKey: { innerKey: 2 } },
            { outerKey: { innerKey: 3 } },
        ],
    },
    {
        items: [
            { outerKey: { innerKey: 3 } },
            { outerKey: { innerKey: 5 } },
            { outerKey: { innerKey: 5 } },
        ],
    },
    {
        items: [
            { outerKey: { innerKey: 1 } },
            { outerKey: { innerKey: 8 } },
            { outerKey: { innerKey: 2 } },
        ],
    },
];


const finalArray = [...new Set(a.reduce((acc, curr) =&gt;  { acc.push(curr.items.map(item =&gt; item.outerKey.innerKey)); return acc;} , []).flat())];

console.log(finalArray);



Is there a better approach than this? considering there could be &gt; 1000 items.
Adding all the elements to a Set should be generally the most efficient way. However, before you add them to the Set you are creating lots of intermediate arrays. It will likely be more efficient to just iterate over the elements and add them to the set one by one:
let set = new Set();
a.forEach(o =&gt; {
    o.items.forEach(i =&gt; {
        set.add(i.outerKey.innerKey);
    });
});
const finalArray = [...set];
console.log(finalArray);

Not sure if forEach() is the fastest way to iterate over the array elements in whatever runtime you are using. See here for a discussion of how to iterate arrays.