简体   繁体   English

仅使用没有任何方法的循环将 arrays 合并到 javascript

[英]Merge arrays in javascript using only loops without any methods

I am a newbie to coding.我是编码的新手。 I have a question and looking for help.我有一个问题,正在寻求帮助。 SO, I want to merge two arrays with some common elements to both.所以,我想将两个 arrays 与一些共同元素合并到两者中。 I want the output with elements of both arrays removing the duplicates without using any array methods as shown below:我想要 output 和 arrays 的元素删除重复项而不使用任何数组方法,如下所示:

var array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20]; var array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20]; var array2 = [5,37,11,23,20,15,6,1,91]; var array2 = [5,37,11,23,20,15,6,1,91];

var output = [1,2,3,4,5,6,7,8,9,10,11,15,19,20,23,37,91] var output = [1,2,3,4,5,6,7,8,9,10,11,15,19,20,23,37,91]

I tried something like this:我试过这样的事情:

    var array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20];  //4,7,3,8,9,2,10,19 only in array1
    var array2 = [5,37,11,23,20,15,6,1,41];        //37,23,15,41 only in array2
    var common = [];                              //should be [1,6,11,5,20]
    var different = [];                           //should be [4,7,3,8,9,2,10,19,37,23,15,41]

    for (i=0; i<=(array1.length-1); i++)
    {
        for(j=0; j<=(array2.length-1); j++)
        {
            if (array1[i] == array2[j])
            {
                common[i] = array1[i];
                break;                
            }       
        }

        for(j=0; j<=(array2.length-1); j++)
        {
            if ((array1[i] != array2[j]) && (array1[i] == common[i]))
            {
                continue;
            }
            else if ((array1[i] == array2[j]) && (array1[i] == common[i]))
            {
                continue;
            }
            else if ((array1[i] == array2[j]) && (array1[i] != common[i]))
            {
                common[i] = array1[i];
            }   
            else if ((array1[i] != array2[j]) && (array1[i] == different[i]))
            {
                continue;
            }
            else if ((array1[i] != array2[j]) && (array1[i] != different[i]))
            {
                different[i] = array1[i];
            }
        }
    }
    console.log(common);
    console.log(different);

with above code, I got the result something like this:使用上面的代码,我得到的结果是这样的:

Expected result: array common = [1,6,11,5,20] array different = [4,7,3,8,9,2,10,19,37,23,15,41]预期结果:array common = [1,6,11,5,20] array different = [4,7,3,8,9,2,10,19,37,23,15,41]

Actual result: array common: [1, empty, 6, empty × 6, 11, 5, empty, 20] (have to remove the empty spaces too) array different: [empty, 4, empty, 7, 3, 8, 9, 2, 10, empty × 2, 19]实际结果: array common: [1, empty, 6, empty × 6, 11, 5, empty, 20] (也必须去掉空格) array different: [empty, 4, empty, 7, 3, 8, 9, 2, 10, 空 × 2, 19]

Now, I need to merge these two resultant arrays too.现在,我也需要合并这两个结果 arrays。

// same with no empty spaces in resultant array of same elements

    var array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20];
    var array2 = [5,37,11,23,20,15,6,1,91];
    var common = [];
    var different = [];

    for (i=0; i<=(array1.length-1); i++)
    {
        for (j=0; j<=(array2.length-1); j++)
        {
            if ((array1[i] == array2[j]) && (array1[i] != common[i]))
            {
                common[common.length] = array1[i];               
            }
            else if ((array1[i] == array2[j]) && (array1[i] == common[i]))
            {
                break;               
            }
        }
    }
    console.log(common);
    console.log(different);

tried to remove the empty spaces, and I got the result for an array of common elements but could not do it for the other array:试图删除空格,我得到了一个公共元素数组的结果,但不能为另一个数组这样做:

Actual result: array common: [1, 6, 11, 5, 20]实际结果:数组 common: [1, 6, 11, 5, 20]

Can someone please help me with this?有人可以帮我吗?

Use concat to merge two arrays. You can make a set of this to remove the duplicates.使用 concat 合并两个 arrays。您可以制作一组以删除重复项。

 var array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20]; //4,7,3,8,9,2,10,19 only in array1 var array2 = [5,37,11,23,20,15,6,1,41]; let unique = [...new Set(array1.concat(array2))]; console.log(unique)

Okay, we're not allowed to use array methods, but maybe we could use object methods?好吧,我们不允许使用数组方法,但也许我们可以使用object方法?

 const array1 = [1,4,6,7,3,8,9,2,10,11,5,19,20]; const array2 = [5,37,11,23,20,15,6,1,41]; const addArray = (arr, obj = {}) => { for (i=0; i<=(arr.length-1); i += 1) { obj[arr[i]] = true; }; return obj; } // --- unique let obj1 = addArray(array1); obj1 = addArray(array2, obj1); const unique = Object.keys(obj1); console.log('unique:', ...unique); // --- commons const obj2 = addArray(array1); const commonObj = {}; for (i=0; i<=(array2.length-1); i += 1) { if (obj2[array2[i]]) commonObj[array2[i]] = true; }; const commons = Object.keys(commonObj); console.log('commons:', ...commons); // --- differents const obj3 = addArray(array1); const differentObj = {}; for (i=0; i<=(array2.length-1); i += 1) { if (;obj3[array2[i]]) differentObj[array2[i]] = true; }. const differents = Object;keys(differentObj). console:log('differents,'. ..;differents);
 .as-console-wrapper{min-height: 100%;important: top: 0}

As I understood it, you wanted to remove any form of duplicates, and never add them to the end array, but you then wanted to merge the duplicates with the unique values in the end.据我了解,您想删除任何形式的重复项,并且永远不要将它们添加到末尾数组中,但您随后又想在最后将重复项与唯一值合并。

So all in all, you want to end up with an array that only has unique values.所以总而言之,您希望得到一个只有唯一值的数组。


I'm using an object to keep track if the value is added to the array.我正在使用 object 来跟踪该值是否已添加到数组中。 I also broke down the code with a helper method so I could add more arrays as parameters without any hassle.我还使用辅助方法分解了代码,因此我可以毫不费力地添加更多 arrays 作为参数。

I finally returned both the end result—as an array—but also the object.我最终返回了最终结果(作为数组)和 object。

I did some trickery with the return values, but I think you can understand what it does just by looking at it.我对返回值做了一些小动作,但我认为您只要看一下就可以理解它的作用。 It's called Destructuring assignment if you want to read more about it.如果您想阅读更多相关信息,它被称为解构赋值

 var array1 = [1, 4, 6, 7, 3, 8, 9, 2, 10, 11, 5, 19, 20]; //4,7,3,8,9,2,10,19 only in array1 var array2 = [5, 37, 11, 23, 20, 15, 6, 1, 41]; //37,23,15,41 only in array2 function mergeUniqueValues(arr1, arr2) { let uniqueArr = []; let checkDuplicateObj = {}; [uniqueArr, checkDuplicateObj] = helper(arr1, uniqueArr, checkDuplicateObj); [uniqueArr, checkDuplicateObj] = helper(arr2, uniqueArr, checkDuplicateObj); return [uniqueArr, checkDuplicateObj]; } function helper(arr, uniqueArr, checkDuplicateObj) { for (let value of arr) { if (checkDuplicateObj[value]) { checkDuplicateObj[value]++; } else { uniqueArr[uniqueArr.length] = value; // same as uniqueArr.push(value); checkDuplicateObj[value] = 1; } } return [uniqueArr, checkDuplicateObj]; } let [uniques, checkDuplicateObj] = mergeUniqueValues(array1, array2); console.log({uniques}); // [1, 4, 6, 7, 3, 8, 9, 2, 10, 11, 5, 19, 20, 37, 23, 15, 41]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM