13 #Handwritten concat method

Use of concat

The concat() method is used to merge two or more arrays. This method does not change the existing array, but returns a new array. If all arguments are omitted, concat returns a shallow copy of the existing array on which this method was called.

<script>
    var arr1 = ["k", "a", "i"];
    var arr2 = ["m", "o"];
    var arr3 = [3, 1, 3];
    var result1 = arr1.concat(arr2, arr3);
    console.log("result1----->", result1);
    var result2 = arr1.concat(arr2, arr3, "hello", 666);
    console.log("result2----->", result2);
</script>

Handwritten concat

<script>
    Array.prototype.kaimoConcat = function (...args) {
      
      
        let newArr = [...this];
        if (args.length === 0) {
      
      
            return newArr;
        }
        args.forEach((el) => {
      
      
            if (Array.isArray(el)) {
      
      
                newArr.push(...el);
            } else {
      
      
                newArr.push(el);
            }
        });
        return newArr;
    };

    var result3 = arr1.kaimoConcat(arr2, arr3);
    console.log("result3---kaimoConcat-->", result3);
    var result4 = arr1.kaimoConcat(arr2, arr3, "hello", 666);
    console.log("result4---kaimoConcat-->", result4);
</script>

Insert image description here

おすすめ

転載: blog.csdn.net/kaimo313/article/details/134320042