Array objects are deduplicated based on key id

        const objArr = [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
            {
    
     id: 3, name: 'Dave' },
            {
    
     id: 2, name: 'Eve' }
        ];

The array is deduplicated based on the key id and the result is:

        [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
        ];

Array objects are deduplicated based on key id

1. Loop traversal

        const objArr = [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
            {
    
     id: 3, name: 'Dave' },
            {
    
     id: 2, name: 'Eve' }
        ];

        const uniqueObjArr = [];
        const idMap = {
    
    };
        objArr.forEach(item => {
    
    
            if (!idMap[item.id]) {
    
    
                idMap[item.id] = true;
                uniqueObjArr.push(item);
            }
        });

        console.log(uniqueObjArr);

Ideas:
1. Define a new array uniqueObjArr to save the object array after deduplication, and define an object idMap to record whether each id appears.
2. Use the forEach method to traverse the original array objArr. For each object item, if the id of the object is not recorded in the idMap, add the object to uniqueObjArr and set the value of the id in the idMap to true.
Note : This method can only remove the id attribute in the object. If there are different objects with the same non-id attribute, they will also be considered different objects.

2. Set data structure

        const objArr = [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
            {
    
     id: 3, name: 'Dave' },
            {
    
     id: 2, name: 'Eve' }
        ];

        const uniqueObjArr = Array.from(new Set(objArr.map(item => item.id)))
            .map(id => {
    
    
                return objArr.find(item => item.id === id);
            });

        console.log(uniqueObjArr);

Ideas:
1. Use the map method to map the objects in objArr to an array containing only the id attribute.
2. Use the Set data structure to deduplicate the array and obtain an array containing only unique ids.
3. Use the map method to find the corresponding object from objArr based on this unique id array, and obtain the deduplicated object array uniqueObjArr.
Note : This method deduplicates all attributes, not specific id attributes, so if there are different objects with the same non-id attributes, they will also be deduplicated. If you need to remove duplicates based on a specific attribute, you can only take this attribute in the map method and use this attribute in the find method for comparison.

3. Use reduce

        const objArr = [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
            {
    
     id: 3, name: 'Dave' },
            {
    
     id: 2, name: 'Eve' }
        ];

        const uniqueObjArr = objArr.reduce((prev, item) => {
    
    
            const index = prev.findIndex(obj => obj.id === item.id);
            if (index === -1) {
    
    
                prev.push(item);
            }
            return prev;
        }, []);

        console.log(uniqueObjArr);

Traverse the original array inside the reduce method. For each object item, use the findIndex method of the array to find whether an object with the same ID exists in the new array. If it does not exist, add it to the new array.
Note : Since the findIndex method is called each time in the loop, the time complexity is high, especially when the array is large, which may affect performance.

4. Use filter

        const objArr = [
            {
    
     id: 1, name: 'Alice' },
            {
    
     id: 2, name: 'Bob' },
            {
    
     id: 1, name: 'Carol' },
            {
    
     id: 3, name: 'Dave' },
            {
    
     id: 2, name: 'Eve' }
        ];

        const uniqueObjArr = objArr.filter((item, index, arr) => {
    
    
            const firstIndex = arr.findIndex(obj => obj.id === item.id);
            return firstIndex === index;
        });

        console.log(uniqueObjArr);

Traverse the original array inside the filter method, use an object to record the position where each id first appears, and then filter based on this position to obtain the deduplicated array.
The advantage of this method is that you don't need to define a new array yourself, and a new array will be returned.
Note : Since the findIndex method is called each time in the loop, the time complexity is high, especially when the array is large, which may affect performance.

Guess you like

Origin blog.csdn.net/Jet_Lover/article/details/130177177