1

Hi I have an array of objects. Each object has an array of objects. I need to find duplicates of (inner) objects that have the same value in a specific property. I made up to create a loop inside the loop and use include.
Is there a shorter way of doing this?

    // Verify that there are no duplicate device names.
    const streamItemNames = [];
    for (let i = 0, length1 = validateStreamItemsResults.streamWebSockets; i < length1; i++) {

        const streamItems = validateStreamItemsResults.streamWebSockets[i].streamItems;

        for (let y = 0, length2 = streamItems.length; y < length2; y++) {

            const streamItem = streamItems[i];
            const streamItemNameLower = streamItem.streamItemName.trim().toLowerCase();

            if (streamItemNames.includes(streamItemNameLower)) {
                validateStreamItemsResults.errorMessage = `Duplicate stream items found with the name: ${streamItemNameLower}`;
                return validateStreamItemsResults;
            } else {
                streamItemNames.push(streamItemNameLower);
            }
        }
    }

UPDATE:
The structure of the objects is the following, for example:
(I need to determine with true or false if there are duplicates "streamItemName" - In the case of this example - true).

const childArray1 = [ { streamItemName: 'Name1' }, { streamItemName: 'Name2' }, { streamItemName: 'Name3' }, { streamItemName: 'Name4' }];
const childArray2 = [ { streamItemName: 'Name5' }, { streamItemName: 'Name6' }, { streamItemName: 'Name7' }, { streamItemName: 'Name1' }];
const parentArray = [childArray1, childArray2];
2
  • 6
    Please show us an example of your object. Commented Feb 28, 2019 at 8:23
  • See stackoverflow.com/help/mcve. What do you mean by "shortest"? In bytes? Commented Feb 28, 2019 at 8:25

1 Answer 1

3

If you're looking for clearer, shorter code, you could use flatMap to extract every streamItemName into a single array, then use .find to find if there are any duplicates:

const streamItemNames = validateStreamItemsResults.streamWebSockets.flatMap(
  socket => socket.streamItems.map(
    item => item.streamItemName.trim().toLowerCase()
  )
);
const dupe = streamItemNames.find((name, i, arr) => arr.slice(i + 1).includes(name));
if (dupe) {
  validateStreamItemsResults.errorMessage = `Duplicate stream items found with the name: ${dupe}`;
  return validateStreamItemsResults;
}

If you don't need to know the duplicate name, you could make it shorter by making a Set and comparing its .size against the array's length.

Sign up to request clarification or add additional context in comments.

3 Comments

Since there might be more than one duplicate wouldn't Arrayprototype.filter() be more fitting? const dupes = streamItemNames.filter((name, i, arr) => arr.slice(i + 1).includes(name)); returning an array of duplicates.
From his code, it looks like OP only wants one duplicate. If he wanted an array instead of just one, then yes, better to .filter
Huh. You are right. He makes it clear that he only need to know whether or not there are any duplicates - in the parenthesis of the updated statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.