Javascript数组ab = ba [英] Javascript arrays ab=ba

查看:247
本文介绍了Javascript数组ab = ba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个多维数组,如: [[a,b],[a,c],[b,a],[b,c],[c,a],[c ,b]] 我如何通过并删除 [a,b] [b ,a]



另外,数组实际上是巨大的,数以万计。 for循环将不得不向后进行,因为数组长度在每次迭代时都会缩小。林甚至不知道每个循环都可以为此工作。我真的不知道如何开始的一个概念。

另外,我试图寻找这一个小时,我甚至不知道如何来解释它。

解决方案

我想我会尝试不同的方法解决这个问题。我也认为它比一些提出的解决方案更快(虽然我们当然需要测试它并进行基准测试)。

首先,为什么我们是否利用了JavaScript数组和对象的面向哈希的特性?我们可以创建一个包含关系的对象(为了创建一种地图),并将这些关系存储在一个新的数组中。使用这种方法,对象也没有问题,我们只是要求一个标识符或散列或任何对象的任何对象。这个标识符必须使它们之间的关系成为可能。

更新


  • 脚本现在控制重复元素fe [[a,b],[a,b]]的可能性
  • 脚本现在控制相同元素的可能性重复的对象fe [[a,a],[a,a] [a,a]]将返回[a,a]


    代码:

      var temp = {},
    massive_arr = [['a','b'] ,['a','c'],['a','d'],['b','a'],['b','c'],['b','d'] ,['c','a'],['c','b'],['c','d']],
    final_arr = [],
    i = 0,
    id1,
    id2;
    for(; i id0 = objectIdentifier(massive_arr [i] [0]); //第一个对象的标识符
    id1 = objectIdentifier(massive_arr [ i)[1]); //第二个对象的标识符

    if(!temp [id0]){//如果该属性不存在于临时对象中,我们创建它。
    temp [id0] = {};
    temp [id0] [id1] = 1;
    } else {//如果存在,我们添加新的密钥。
    temp [id0] [id1] = 1;


    if(id0 === id1&&!temp [id0] [id1 +_ bis]){//特殊情况[a,a]
    temp [id0] [id1 +_ bis] = 1;
    final_arr.push(massive_arr [i]);
    continue; //跳转到下一个迭代
    }

    if(!temp [id1]){//存储元素并将其标记为已存储。
    temp [id1] = {};
    temp [id1] [id0] = 1;
    final_arr.push(massive_arr [i]);
    continue; //跳转到下一个迭代
    }

    if(!temp [id1] [id0]){//存储元素并将其标记为已存储。
    temp [id1] [id0] = 1;
    final_arr.push(massive_arr [i]);
    }
    }
    console.log(final_arr);

    函数objectIdentifier(obj){
    return obj; //您必须返回对象的有效标识符。例如,obj.id或obj.hashMap ...无论如何明确标识它。



    $ b

    你可以测试它 here
    $ b 第二次更新



    虽然这不是我们要求的,但是我已经改变了方法来适应n长度的元素(如果需要,n可以改变)。

    这个方法比较慢,因为它依赖排序为map生成一个有效的键。即使如此,我认为这也足够快。

      var temp = {},
    massive_arr = [
    ['a','a','a'],// 0
    ['a','a','b'],// 1
    ['a','b' ,'a'],
    ['a','a','b'],
    ['a','c','b'],// 2
    [ 'a','c','d'],// 3
    ['b','b','c'],// 4
    ['b','b', 'b'],// 5
    ['b','b','b'],
    ['b','c','b'],
    [ b','c','d'],// 6
    ['b','d','a'],// 7
    ['c','d',' b'],
    ['c','a','c'],// 8
    ['c','c','a'],
    ['c '','d','a','j'],// 9
    ['c','d','a','j','k'],// 10
    ['c','d','a','o'],// 11
    ['c','d','a']
    ],
    final_arr = [],
    i = 0,
    j,
    ord,
    key;
    for(; i< massive_arr.length; i ++){
    ord = [];
    for(j = 0; j ord.push(objectIdentifier(massive_arr [i] [j]));
    }

    ord.sort();
    key = ord.toString();

    if(!temp [key]){
    temp [key] = 1;
    final_arr.push(massive_arr [i]);
    }
    }

    console.log(final_arr);

    函数objectIdentifier(obj){
    return obj;
    }

    可以测试这里


    If i have a multidimensional array like: [[a,b],[a,c],[b,a],[b,c],[c,a],[c,b]] how can i go through and remove repeats where [a,b] is the same as [b,a].

    also, the array is actually massive, in the tens of thousands. A for loop would have to be done backwards because the array length will shrink on every iteration. Im not even sure that an each loop would work for this. I really am at a loss for just a concept on how to begin.

    Also, i tried searching for this for about an hour, and i don't even know how to phrase it.

    解决方案

    I think I'm going to try a different approach to this problem. I also think it'll be quicker than some of the solutions proposed (though we'd need of course to test it and benchmark it).

    First off, why don't we take advantage of the hash oriented nature of javascript arrays and objects? We could create an object containing the relations (in order to create a kind of a map) and store in a new array those relationships that hasn't been stored yet. With this approach there's no problem about objects either, we just request for an identifier or hash or whatever for every object. This identifier must make the relationship between them possible.

    UPDATE

    • The script now controls the possibility of repeated elements f.e [[a,b],[a,b]]
    • The script now controls the possibility of elements with the same object repeated f.e [[a,a],[a,a][a,a]] would return [a,a]

    The code:

    var temp = {},
        massive_arr = [['a','b'],['a','c'],['a','d'], ['b','a'],['b','c'],['b','d'],['c','a'],['c','b'],['c','d']],
        final_arr = [],
        i = 0,
        id1,
        id2;
    for( ; i < massive_arr.length; i++ ) {
        id0 = objectIdentifier(massive_arr[i][0]);// Identifier of first object
        id1 = objectIdentifier(massive_arr[i][1]);// Identifier of second object
    
        if(!temp[id0]) {// If the attribute doesn't exist in the temporary object, we create it.
            temp[id0] = {};
            temp[id0][id1] = 1;
        } else {// if it exists, we add the new key.
            temp[id0][id1] = 1;
        }
    
        if( id0 === id1 && !temp[id0][id1+"_bis"] ) {// Especial case [a,a]
            temp[id0][id1+"_bis"] = 1;
            final_arr.push(massive_arr[i]);
            continue;// Jump to next iteration
        }
    
        if (!temp[id1]) {// Store element and mark it as stored.
          temp[id1] = {};
          temp[id1][id0] = 1;
          final_arr.push(massive_arr[i]);
          continue;// Jump to next iteration
        }
    
        if (!temp[id1][id0]) {// Store element and mark it as stored.
          temp[id1][id0] = 1;
          final_arr.push(massive_arr[i]);
        }
    }
    console.log(final_arr);
    
    function objectIdentifier(obj) {
        return obj;// You must return a valid identifier for the object. For instance, obj.id or obj.hashMap... whatever that identifies it unequivocally.
    }
    

    You can test it here

    SECOND UPDATE

    Though this is not what was requested in the first place, I've changed the method a bit to adapt it to elements of n length (n can vary if desired).

    This method is slower due to the fact that relies on sort to generate a valid key for the map. Even so, I think it's fast enough.

    var temp = {},
    massive_arr = [
        ['a', 'a', 'a'], //0
        ['a', 'a', 'b'], //1
        ['a', 'b', 'a'],
        ['a', 'a', 'b'],
        ['a', 'c', 'b'], //2
        ['a', 'c', 'd'], //3
        ['b', 'b', 'c'], //4
        ['b', 'b', 'b'], //5
        ['b', 'b', 'b'],
        ['b', 'c', 'b'],
        ['b', 'c', 'd'], //6
        ['b', 'd', 'a'], //7
        ['c', 'd', 'b'],
        ['c', 'a', 'c'], //8
        ['c', 'c', 'a'],
        ['c', 'd', 'a', 'j'], // 9
        ['c', 'd', 'a', 'j', 'k'], // 10
        ['c', 'd', 'a', 'o'], //11
        ['c', 'd', 'a']
    ],
        final_arr = [],
        i = 0,
        j,
        ord,
        key;
    for (; i < massive_arr.length; i++) {
        ord = [];
        for (j = 0; j < massive_arr[i].length; j++) {
            ord.push(objectIdentifier(massive_arr[i][j]));
        }
    
        ord.sort();
        key = ord.toString();
    
        if (!temp[key]) {
            temp[key] = 1;
            final_arr.push(massive_arr[i]);
        }
    }
    
    console.log(final_arr);
    
    function objectIdentifier(obj) {
        return obj;
    }
    

    It can be tested here

    这篇关于Javascript数组ab = ba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆