This is the sixth day of my participation in the August Text Challenge.More challenges in August

Array to heavy

(new Set && array. from)

  • New Set() and array.from () are the ones I use most often
  • Pros: Simple to use
  • Disadvantages: Cannot deduplicate array objects

Basic usage:

const sourceArray = [1.2.3.4.5.4.3.2.1];
const targetArray = Array.from(new Set(sourceArray));
console.log(targetArray,'targetArray')
[1, 2, 3, 4, 5]
Copy the code

The second (find)

  • find
  • Loop to determine whether it is repeated
// Array of numbers or strings (not objects)
const sourceNumArray = [1.2.3.4.5.4.3.2.1];
let targetNumArray = [];
sourceNumArray.forEach(item= > {
  const findItem = targetNumArray.find(child= > child === item);
  // Check if you already own it and push it if you don't
  if(! findItem) targetNumArray.push(item) })console.log(targetNumArray, 'targetNumArray');

[1, 2, 3, 4, 5]



// Find unduplicate (array of objects)

// Array (object)
    const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
    let targetObjectArray = [];

    sourceObjectArray.forEach(item= > {
     const findItem = targetObjectArray.find(child= > JSON.stringify(child)=== JSON.stringify(item));
        if(! findItem) targetObjectArray.push(item) })console.log(targetObjectArray,'targetObjectArray')
 
 / / output: [{name: "xiao Ming, id: 1}, {name:" little red ", id: 2}, {name: 'lil' id: 3}]

Copy the code

Encapsulate find as a common deduplication function

  • Compatible array object
  • Compatible array strings/numbers
  • Compatible with de-duplicating a field of an object
 / * * *@dec The find function deduplicated *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
    function findDeDuplicationPack(source, key) {
        let target = [];
        source.forEach((item) = > {
            const findItem = target.find(child= > {
                if(! key &&JSON.stringify(child) === JSON.stringify(item)) {
                    return child
                }else if(key && child[key] === item[key]){
                   return child
                }
            });

            if(! findItem) target.push(item) })return target;
    }
const sourceNumArray = [1.2.3.4.5.4.3.2.1];
const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]

Log (findDeDuplicationPack(sourceObjectArray),'sourceObjectArray')
Console. log(findDeDuplicationPack(sourceNumArray),'sourceNumArray')
FindDeDuplicationPack (sourceObjectArray2, 'id'),'sourceObjectArray2')
// The output is correct
Copy the code

The third kind of (some)

  • some
  • Also cyclic judgment uses some methods to implement reprocessing

The code implementation is as follows:

 // Array (not object)
   const sourceNumArray = [1.2.3.4.5.4.3.2.1];
   let targetNumArray = [];
   sourceNumArray.forEach(item= > {
       const findItem = targetNumArray.some(child= > child === item);
       if(! findItem) targetNumArray.push(item) })console.log(targetNumArray, 'targetNumArray');


     // Array (object)
   const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
   const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
   let targetObjectArray = [];
    

   sourceObjectArray.forEach(item= > {
       const findItem = targetObjectArray.some(child= > JSON.stringify(child) === JSON.stringify(item));
       if(! findItem) targetObjectArray.push(item) })console.log(targetObjectArray,'targetObjectArray')



   // Function encapsulation
   / * * *@dec The some function deduplicates *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
   function someDeDuplicationPack(source, key) {
       let target = [];
       source.forEach((item) = > {
           const findItem = target.some(child= > {
               if(! key &&JSON.stringify(child) === JSON.stringify(item)) {
                   return child
               }else if(key && child[key] === item[key]){
                  return child
               }
           });

           if(! findItem) target.push(item) })return target;
   }
 
   console.log(someDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
   console.log(someDeDuplicationPack(sourceNumArray),'sourceNumArray')
   console.log(someDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

A fourth (findIndex)

  • FindIndex is similar to the previous two except that the criteria are different
   // Array (not object)
   const sourceNumArray = [1.2.3.4.5.4.3.2.1];
   let targetNumArray = [];
   sourceNumArray.forEach(item= > {
       const findItem = targetNumArray.findIndex(child= > child === item);
       if(findItem === -1) targetNumArray.push(item)
   })

   console.log(targetNumArray, 'targetNumArray');


   // Array (object)
   const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
   const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
   let targetObjectArray = [];
    

   sourceObjectArray.forEach(item= > {
       const findItem = targetObjectArray.findIndex(child= > JSON.stringify(child) === JSON.stringify(item));
       if(findItem === -1) targetObjectArray.push(item)
   })

   console.log(targetObjectArray,'targetObjectArray')



   // Function encapsulation
   / * * *@dec The findIndex function removes the *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
   function findIndexDeDuplicationPack(source, key) {
       let target = [];
       source.forEach((item) = > {
           const findItem = target.findIndex(child= > {
               if(! key &&JSON.stringify(child) === JSON.stringify(item)) {
                   return child
               }else if(key && child[key] === item[key]){
                  return child
               }
           });

           if(findItem === -1) target.push(item)
       })

       return target;
   }
 
   console.log(findIndexDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
   console.log(findIndexDeDuplicationPack(sourceNumArray),'sourceNumArray')
   console.log(findIndexDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

Fifth (every)

  • Every uses every to implement de-weighting

Type 6 (includes)

  • Includes implements array de-duplication
    // Array (not object)
    const sourceNumArray = [1.2.3.4.5.4.3.2.1];
    let targetNumArray = [];
    sourceNumArray.forEach(item= > {
        const findItem = targetNumArray.includes(item);
        if(! findItem) targetNumArray.push(item) })console.log(targetNumArray, 'targetNumArray');


    // Array (object)
    const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
    const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
    let targetObjectArray = [], targetObjectStringArray = [];
    

    sourceObjectArray.forEach(item= > {
        const findItem = targetObjectStringArray.includes(JSON.stringify(item))
        if(! findItem) { targetObjectArray.push(item); targetObjectStringArray.push(JSON.stringify(item))
        }
    })

    console.log(targetObjectArray,'targetObjectArray')



    // Function encapsulation
    / * * *@dec The includes function deletes *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
    function includesDeDuplicationPack(source, key) {
        let target = [], targetString = [];
        source.forEach((item) = > {
            const findItem = targetString.includes(JSON.stringify(key ? item[key] : item))

            if(! findItem) { target.push(item); targetString.push(JSON.stringify(key ? item[key] : item))
            }
        })

        return target;
    }
  
    console.log(includesDeDuplicationPack(sourceObjectArray),'sourceObjectArray')
    console.log(includesDeDuplicationPack(sourceNumArray),'sourceNumArray')
    console.log(includesDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

No.7 (filter)

  • Filter implements array deduplication
// Array (not object)
const sourceNumArray = [1.2.3.4.5.4.3.2.1];
let targetNumArray = [];
sourceNumArray.forEach(item= > {
    const filterItem = targetNumArray.filter(child= > child === item);
    if(! filterItem.length) targetNumArray.push(item) })console.log(targetNumArray, 'targetNumArray');


// Array (object)
const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
let targetObjectArray = [];
 

sourceObjectArray.forEach(item= > {
    const filterItem = targetObjectArray.filter(child= > JSON.stringify(child) === JSON.stringify(item));
    if(! filterItem.length) targetObjectArray.push(item) })console.log(targetObjectArray,'targetObjectArray')



// Function encapsulation
/ * * *@dec The filter function deduplicates *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
function filterDeDuplicationPack(source, key) {
    let target = [];
    source.forEach((item) = > {
        const filterItem = target.filter(child= > {
            if(! key &&JSON.stringify(child) === JSON.stringify(item)) {
                return child
            }else if(key && child[key] === item[key]){
               return child
            }
        });

        if(! filterItem.length) target.push(item) })return target;
}

console.log(filterDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
console.log(filterDeDuplicationPack(sourceNumArray),'sourceNumArray')
console.log(filterDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

Type 8 (MAP)

  • Map implements array deduplication
   // Array (not object)
const sourceNumArray = [1.2.3.4.5.4.3.2.1];
let targetNumArray = [];
sourceNumArray.forEach(item= > {
    const mapItem = targetNumArray.map(child= > child === item);

    if(mapItem.every(child= >! child) || mapItem.length ===0) targetNumArray.push(item)
})

console.log(targetNumArray, 'targetNumArray');


// Array (object)
const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
let targetObjectArray = [];
 

sourceObjectArray.forEach(item= > {
    const mapItem = targetObjectArray.map(child= > JSON.stringify(child) === JSON.stringify(item));
    if(mapItem.every(child= >! child) || mapItem.length ===0) targetObjectArray.push(item)
})

console.log(targetObjectArray,'targetObjectArray')



// Function encapsulation
/ * * *@dec The map function is stripped of the *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
function mapDeDuplicationPack(source, key) {
    let target = [];
    source.forEach((item) = > {
        const mapItem = target.map(child= > {
            if(! key &&JSON.stringify(child) === JSON.stringify(item)) {
                return true
            }else if(key && child[key] === item[key]){
               return true}});if(mapItem.every(child= >! child) || mapItem.length ===0) target.push(item)
    })

    return target;
}

console.log(mapDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
console.log(mapDeDuplicationPack(sourceNumArray),'sourceNumArray')
console.log(mapDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

indexOf

  • IndexOf implements array de-duplication
       // Array (not object)
    const sourceNumArray = [1.2.3.4.5.4.3.2.1];
    let targetNumArray = [];
    sourceNumArray.forEach(item= > {
        const findItem = targetNumArray.indexOf(item);
        if(findItem === -1) targetNumArray.push(item)
    })

    console.log(targetNumArray, 'targetNumArray');


    // Array (object)
    const sourceObjectArray = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}]
    const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]
    let targetObjectArray = [], targetObjectStringArray = [];
    

    sourceObjectArray.forEach(item= > {
        const findItem = targetObjectStringArray.indexOf(JSON.stringify(item))
        if(findItem === -1) {
            targetObjectArray.push(item);
            targetObjectStringArray.push(JSON.stringify(item))
        }
    })

    console.log(targetObjectArray,'targetObjectArray')



    // Function encapsulation
    / * * *@dec IndexOf removes the *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */
    function indexOfDeDuplicationPack(source, key) {
        let target = [], targetString = [];
        source.forEach((item) = > {
            const findItem = targetString.indexOf(JSON.stringify(key ? item[key] : item))

            if(findItem === -1) {
                target.push(item);
                targetString.push(JSON.stringify(key ? item[key] : item))
            }
        })

        return target;
    }
  
    console.log(indexOfDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
    console.log(indexOfDeDuplicationPack(sourceNumArray),'sourceNumArray')
    console.log(indexOfDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

10 (more elegant, optimized performance, fast new Map)


 const sourceNumArray = [1.2.3.4.5.4.3.2.1];

  let sourceNumMap = new Map(a);let targetNumArray = [];
    sourceNumArray.forEach(item= > {

        if(! sourceNumMap.get(item)) { targetNumArray.push(item); sourceNumMap.set(item, item); } }) sourceNumMap.clear()// Free up space
    console.log(targetNumArray, 'targetNumArray');


    const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]

   // Function encapsulation
    / * * *@dec The new Map function removes the *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */

     function newMapDeDuplicationPack(source, key) {
        let target = [];
        let targetMap = new Map()
        source.forEach((item) = > {

            const setItem = key ? JSON.stringify(item[key]) : JSON.stringify(item);

            if(! targetMap.get(setItem)) { target.push(item); targetMap.set(setItem, item) } }) targetMap.clear();return target;
    }

console.log(newMapDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
console.log(newMapDeDuplicationPack(sourceNumArray),'sourceNumArray')
console.log(newMapDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')
Copy the code

Eleventh (more elegant, optimized performance, fast new Set)

  • New Set() implements array de-duplication
  const sourceNumArray = [1.2.3.4.5.4.3.2.1];

  let sourceNumSet = new Set(a);let targetNumArray = [];
    sourceNumArray.forEach(item= > {

        if(! sourceNumSet.has(item)) { targetNumArray.push(item); sourceNumSet.add(item); } }) sourceNumSet.clear()// Free up space
    console.log(targetNumArray, 'targetNumArray');


    const sourceObjectArray2 = [{name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'lil'.id: 3}, {name: 'Ming'.id: 1}, {name: 'little red'.id: 2}, {name: 'black'.id: 2}]


   // Function encapsulation
    / * * *@dec The new Set function removes the *@params Source {Array} Mandatory source Array *@params Key {String} is not mandatory (a field of the array object is erased) *@return Deduplicated array */

     function newSetDeDuplicationPack(source, key) {
        let target = [];
        let targetSet = new Set(a); source.forEach((item) = > {

            const setItem = key ? JSON.stringify(item[key]) : JSON.stringify(item);

            if(! targetSet.has(setItem)) { target.push(item); targetSet.add(setItem) } }) targetSet.clear();return target;
    }

console.log(newSetDeDuplicationPack(sourceObjectArray2),'sourceObjectArray')
console.log(newSetDeDuplicationPack(sourceNumArray),'sourceNumArray')
console.log(newSetDeDuplicationPack(sourceObjectArray2, 'id'),'sourceObjectArray2')

Copy the code

conclusion

  • Hi, I am Mihara and thank you for watching and I will work harder.
  • Each method is typed out and verified, and can be copied if necessary.
  • If you give help a thumbs-up 👍 is even better thank you ~~~~~
  • We look forward to your attention