重构复杂的嵌套数组 [英] Restructure complex nested array

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

问题描述

我有这样一个数组:

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坐标 [18, 4]

The arrays in my_array includes another two arrays (the first is not that necessary, but looking at the second (myarray[1] : This array includes different x/y-coordinates [18, 4])

我想得到另一个看起来像这样的数组(下面的解释):

I'd like to get as result another array that looks like this (explanation below):

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值: 9 )并在新数组中分组.x-va之间的差异可以说,lues可以是+/- 2个索引(x值7,8, 9 ,10,11)可以在一个组中。

Arrays are now sorted by their x-values ([9, 10] --> x-value: 9) and grouped in new arrays. The difference between the x-values can be, lets say, +/- 2 indexes (x-values 7,8,9,10,11) could be in one group.

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

I have no idea how to code this; this is what I have so far:

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值差异应该分组的坐标不应大于 1 。请看下面的例子:

Edit: One point I forgot to mention, is that the y-value difference of the coordinates that should be grouped should not be bigger than 1. Look at the example below:

(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号线和1号线。线和&第2行:x值(第2行:'11'和第1行:'9','10','9'将完美匹配。现在我也想分开y值,就像我在例子中那样上面编辑过。
- >即使 x值一起匹配,如果 y-,它们也应该被分组到新的数组中值匹配在一起

If you have a look at line 1 & line & line 2: The x values (line2: '11' & line 1: '9','10','9' would match perfectly together. Now I also want to separate the y-values like in the example I've edited above. --> Even if the x-values match together, they should just be grouped into new arrays if there y-values match together.

Y值匹配意味着,有一个类似行 - >
(x:2, y:4 ),(x:1, y:5 ),(x:2, y:6 ),(x:2 , y:7 )而不是像(x:4, y:42 )这样的值

Y-value match means, that there is something like a row -->
(x: 2, y:4),(x: 1, y:5), (x: 2, y:6), (x: 2, y:7) and not values like (x: 4, y:42)

我希望我的编辑让我更容易理解我的想法..

I hope my edit made it easier to understand my thoughts..

提前致谢,Jonas

Thanks in advance, Jonas

推荐答案

编辑:格式和代码样式

编辑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 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; }

这篇关于重构复杂的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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