重组复合物嵌套阵列



我有一个类似的数组:

var my_array= [
    [2, [[9, 10]]],
    [5, [[10, 11]]],
    [4, [[11, 9]]],
    [1, [[19, 2], [41, 10]]],
    [7, [[17, 3]]],
    [0, [[11, 4], [18, 5]]]
]

my_array中的数组包括另一个数组(第一个数组不是必需的,而是查看第二个数组(myarray[1]:此数组包含不同的X/Y-COORDINATES [18, 4]

我想作为结果获得另一个看起来像这样的数组(下面说明):

var result_array= [
    [ [2, [9, 10]], [5, [10, 11]], [4, [11, 9]], [0, [11, 4]] ],
    [ [1, [19, 2]],  [0, [18, 5]] , [7, [17, 3]] ],    
    [ [1, [41, 10]] ]
]

现在由其X值([9, 10]-> X-Value:9)对数组进行排序,并将其分组为新数组。可以的说,X值之间的区别可以是/- 2索引(x-values 7,8, 9 ,10,11)可以在一个组中。

我不知道如何编码;这就是我到目前为止的:

var my_array= [
        [2, [[9, 10]]],
        [5, [[10, 11]]],
        [4, [[11, 9]]],
        [1, [[19, 2], [41, 10]]],
        [7, [[17, 3]]],
        [0, [[11, 4], [18, 5]]]
    ]
function sortArray(array) {
  var difference = 2,
    result = '';
  var array_sorted = [];
    array.forEach(function(a) {
     a[1].forEach(function(b) {
       array_sorted.push([b[0],b[1],a[0]]);
    })
  })
  array_sorted = array_sorted.sort(function(a,b) {return a[0]-b[0]});
  
  array_sorted.forEach(function(a) {
    if (a[0] > difference) {
    difference = a[0];
     array_sorted.push(array_group); array_group = [];}
     array_group.push([a]);
  })
  return array_sorted;
}
console.log(sortArray(my_array));


编辑:我忘了提到的一个点是,应该分组的坐标的y-value difference不应大于1。查看下面的示例:

(x: 3, y:1),(x: 1, y:2),(x: 2, y:3),(x: 4, y:4)-> not (x: 4, y:41)

编辑2

var my_array= [
    [2, [[9, 10]]],
    [5, [[10, 11]]],
    [4, [[11, 9]]],
    [1, [[19, 2], [41, 10]]],   
    [7, [[17, 3]]],
    [0, [[11, 4], [18, 5]]]
]
var result_array= [
    [ [2, [9, 10]], [5, [10, 11]], [4, [11, 9]] ], // line 1
    [ [0, [11, 4]] ],                              // line 2 
    [ [1, [19, 2]] ],                              // line 3
    [ [7, [17, 3]] ],                              // line 4 
    [ [0, [18, 5]] ],                              // line 5 
    [ [1, [41, 10]] ]                              // line 6 
]

如果您查看第1行&线&第2行:x值(line2:'11'&第1:'9','10','9'将完美匹配。现在我也想将y值分开在上面编辑。 ->即使x-values match together,如果有y-values match together,它们也应将其分组为新数组。

y-value匹配意味着,有类似一行的东西 ->
(x:2, y:4 ),(x:1, y:5),(x:2, y:6 ),(x:2, y:7 ),而不是(x:x:4,<strong)> y:42 )

我希望我的编辑使理解我的想法变得更容易。

提前感谢,乔纳斯

编辑:格式和代码样式

edit2:对原始问题编辑的回答

var my_array= [
  [2, [[9, 10]]],
  [5, [[10, 11]]],
  [4, [[11, 9]]],
  [1, [[19, 2], [41, 10]]],   
  [7, [[17, 3]]],
  [0, [[11, 4], [18, 5]]]
]
    var xdifference = 2;
    var ydifference = 1;
  function split(input_array) {
    var splitted = [];
    input_array.forEach(function (item) {
      var coordinates = item[1];
      coordinates.forEach(function (coordinate) {
        splitted.push([item[0], coordinate]);
      });
    });
    return splitted;
  }
  function getXValueOf(item) {
    return item[1][0];
  }
  function getYValueOf(item) {
    return item[1][1];
  }
  function divideIntoHeaps(sorted_array) {
    var heaps = [];
    function findMatchingHeap(item) {
       var matching = heaps.find(function (heap) {
         return heap.every(function (itemOfHeap) {
           var xMatches = Math.abs(getXValueOf(item) - getXValueOf(itemOfHeap)) <= xdifference+1;
           var yMatches = Math.abs(getYValueOf(item) - getYValueOf(itemOfHeap)) <= ydifference+1;
           return xMatches && yMatches;
         });
       });
       return matching;
    }
    function allocate(item) {
      if (heaps.length == 0) {
        heaps.push([item]);
      } else {
        var matchingHeap = findMatchingHeap(item);
        if (matchingHeap !== undefined) {
          matchingHeap.push(item);
        } else {
          heaps.push([item]);
        }
      }
    }
    sorted_array.forEach(allocate);
    return heaps;
  }
  function sortArray(my_array) {
    var splitted = split(my_array);
    var result = divideIntoHeaps(splitted);
    return result;
  }
  
  var result = sortArray(my_array);
  result.forEach( function (row) {
     console.log(JSON.stringify(row));
  });
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果我们命名了输入[[z,[[x,y],[x,y]],...]的各个部分,则此代码将其变平[[x,y,z],...],由x进行分类,然后将其块返回组为组:

var my_array= [
    [2, [[9, 10]]],
    [5, [[10, 11]]],
    [4, [[11, 9]]],
    [1, [[19, 2], [41, 10]]],
    [7, [[17, 3]]],
    [0, [[11, 4], [18, 5]]]
]
var rearrange = function(arr) {
  // flatten the input structure into [[x,y,z], [x,y,z]]:
  var simpler = [];
  arr.forEach(function(row) {
    row[1].forEach(function(pair) {
      simpler.push([pair[0],pair[1],row[0]]);
    })
  });
  // sort by x:
  simpler = simpler.sort(function(a,b) {
    return a[0]-b[0]
  });
  // console.log(simpler);
  // Now group by x±2, into [ [z,[x,y]],[z,[x,y]] ]
  var output = [];
  var group = [];
  var latestX=simpler[0][0];
  simpler.forEach(function(row) {
    if (row[0] > latestX + 5) {
      // start a new group
      latestX = row[0];
      if (group.length > 0) {
        output.push(group);
        group = [];
      }
    }
    group.push([row[2],[row[0],row[1]]]);  
  });
  output.push(group); // catch the last group
  return output;
}
console.log(rearrange(my_array));

您可以单点并检查所有组是否在组节点的通道范围内。如果不是任何组,请生成一个新组并存储点。继续直到没有更多点可用。

var array = [[2, [[9, 10]]], [5, [[10, 11]]], [4, [[11, 9]]], [1, [[19, 2], [41, 10]]], [7, [[17, 3]]], [0, [[11, 4], [18, 5]]]],
    normalized = array.reduce(function (r, a) {
        return r.concat(a[1].map(function (b) { return [a[0], b]; }));
    }, []),
    grouped = [],
    i, j, k,
    updated;
loop: while (normalized.length) {
    if (!updated) {
        grouped.push([normalized.shift()]);
    }
    updated = false;
    for (i = 0; i < normalized.length; i++) {
        for (j = 0; j < grouped.length; j++) {
            for (k = 0; k < grouped[j].length; k++) {
                if (Math.abs(normalized[i][1][0] - grouped[j][k][1][0]) <= 2 && Math.abs(normalized[i][1][1] - grouped[j][k][1][1]) <= 1) {
                    grouped[j].push(normalized.splice(i, 1)[0]);
                    updated = true;
                    continue loop;
                }
            }
        }
    }
}
console.log(grouped.map(function (a) { return JSON.stringify(a); }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

仅适用于一个dimenstion的较旧版本。

您可以通过

进行直接的方法
  • 归一化值,
  • 排序值,
  • 分组三角洲较小或等于两个。

var array = [[2, [[9, 10]]], [5, [[10, 11]]], [4, [[11, 9]]], [1, [[19, 2], [41, 10]]], [7, [[17, 3]]], [0, [[11, 4], [18, 5]]]],
    grouped = array
        .reduce(function (r, a) {
            return r.concat(a[1].map(function (b) { return [a[0], b]; }));
        }, [])
        .sort(function (a, b) {
            return a[1][0] - b[1][0];
        })
        .reduce(function (r, a, i, aa) {
            if (aa[i - 1] && a[1][0] - aa[i - 1][1][0] <= 2) {
                r[r.length - 1].push(a);
            } else {
                r.push([a]);
            }
            return r;
        }, []);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新