在JS

时间:2018-03-07 13:15:44

标签: javascript arrays sorting

我有一个数组

var arr= [
    ["PROPRI","PORVEC"],
    ["AJATRN","PROPRI"], 
    ["BASMON","CALVI"],
    ["GHICIA","FOLELI"],
    ["FOLELI","BASMON"],
    ["PORVEC","GHICIA"]
] ;

我试图通过使第二个元素等于下一个元素的第一个元素来对数组进行排序,如下所示:

arr = [
        ["AJATRN","PROPRI"],
        ["PROPRI","PORVEC"], 
        ["PORVEC","GHICIA"],
        ["GHICIA","FOLELI"],
        ["FOLELI","BASMON"],
        ["BASMON","CALVI"]
]

上下文是:这些是带坐标的网站,我想识别传递的顺序,

例如,我有[A,B] [C,D] [B,C]然后我知道路径是A B C D

我终于有了一个解决方案

var rs =[]; 
rs[0]=arr[0];
var hasAdded=false;
for (var i = 1; i < arr.length; i++) {
        hasAdded=false;
        console.log("i",i);
        for (var j = 0, len=rs.length; j < len; j++) {
            console.log("j",j);
            console.log("len",len);
            if(arr[i][1]===rs[j][0]){
                rs.splice(j,0,arr[i]);
                hasAdded=true;
                 console.log("hasAdded",hasAdded);
            } 
            if(arr[i][0]===rs[j][1]){
                rs.splice(j+1,0,arr[i]);
                hasAdded=true;
                console.log("hasAdded",hasAdded);
            }
        }

        if(hasAdded===false) {
            arr.push(arr[i]);
             console.log("ARR length",arr.length);
        }
    }

但是当它像[A,B] [B,C] [C,D] [D,A]这样的圆圈时,它并不完美 我无法得到除了答案

2 个答案:

答案 0 :(得分:1)

所以我真的希望这是你想要实现的,所以看看这个简单的js代码:

&#13;
&#13;
<?php ?>
&#13;
&#13;
&#13;

注意:当然可以使用<?php // Add custom size of thumbnails if ( function_exists( 'add_theme_support' ) ) { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped) add_image_size( 'another-size', 600, 400 ); // Not Cropped (additional sizes) size doesn't matter because we're adding "full" } // Fixes Media Library error (without this it's not possible to add images or manage it – it just appears empty) function ms_image_editor_default_to_gd( $editors ) { $gd_editor = 'WP_Image_Editor_GD'; $editors = array_diff( $editors, array( $gd_editor ) ); array_unshift( $editors, $gd_editor ); return $editors; } add_filter( 'wp_image_editors', 'ms_image_editor_default_to_gd' ); // Created three different sidebars and allows user to edit their content as widgets if ( function_exists('register_sidebar') ) { register_sidebar(array( 'name' => 'column 1', 'before_widget' => '', 'after_widget' => '', 'before_title' => '<h2>', 'after_title' => '</h2>' )); register_sidebar(array( 'name' => 'column 2', 'before_widget' => '', 'after_widget' => '', 'before_title' => '<h2>', 'after_title' => '</h2>' )); register_sidebar(array( 'name' => 'column 3', 'before_widget' => '', 'after_widget' => '', 'before_title' => '<h2>', 'after_title' => '</h2>' )); }?> 轻松实现此结果,但由于您的问题,我非常确定这会让您感到困惑。那么看看使用for循环完成的代码:)

答案 1 :(得分:1)

您可以根据数组的第一个值创建对象查找。使用此查找,您可以获取第一个键,然后开始为结果添加值。在数组中添加值后,如果键中没有元素,则删除该键对应的值,删除其键。只要在对象查找中有键,就继续此过程。

var vector = [["PROPRI", "PORVEC"],["AJATRN", "PROPRI"],["BASMON", "CALVI"],["GHICIA", "FOLELI"],["FOLELI", "BASMON"],["PORVEC", "GHICIA"]],
    lookup = vector.reduce((r,a) => {
      r[a[0]] = r[a[0]] || [];
      r[a[0]].push(a);
      return r;
    }, {});
var current = Object.keys(lookup).sort()[0];
var sorted = [];

while(Object.keys(lookup).length > 0) {
  if(lookup[current] && lookup[current].length) {
    var first = lookup[current].shift();
    sorted.push(first);
    current = first[1];
  } else {
    delete lookup[current];
    current = Object.keys(lookup).sort()[0];
  }
}
console.log(sorted);