Javascript 为什么我的数组没有按我将对象推入数组的方式排序?

Javascript 为什么我的数组没有按我将对象推入数组的方式排序?,javascript,arrays,json,Javascript,Arrays,Json,好的,我对javascript对象数组有一个小问题,数组看起来像这样: var myArray = [ {Month : 1, Count : 1000}, {Month : 2, Count : 1000}, {Month : 9, Count : 1000}, {Month : 10, Count : 1000}, {Month : 11, Count : 1000},

好的,我对javascript对象数组有一个小问题,数组看起来像这样:

var myArray = [
           {Month : 1, Count : 1000},
           {Month : 2, Count : 1000},
           {Month : 9, Count : 1000},
           {Month : 10, Count : 1000},
           {Month : 11, Count : 1000},
           {Month : 12, Count : 1000},
     ]; 
________________________________________________
 09       10       11       12        1        2
var date = new Date();
var currentMonth = date.getMonth()+1; // months go from 0 to 11, but your code is 1-12

// add the new ordering parameter
for (var x = 0; x < myArray.length; x++) {
    if (myArray[x].Month > currentMonth) {
        myArray[x].Order = myArray[x].Month - 12;
    } else {
        myArray[x].Order = myArray[x].Month;
    }
}

// sort the array using the new Order value
myArray.sort(function compare(a,b) {
              if (a.Order < b.Order)
                 return -1;
              else
                 return 1;
            });
我遇到的问题是,我正在使用这个对象数组来显示数据 一个图表。在我的一生中,我尝试了所有我能想到的方法,以我需要的方式重新排列这个数组。需要是这样的:

var myArray = [
           {Month : 1, Count : 1000},
           {Month : 2, Count : 1000},
           {Month : 9, Count : 1000},
           {Month : 10, Count : 1000},
           {Month : 11, Count : 1000},
           {Month : 12, Count : 1000},
     ]; 
________________________________________________
 09       10       11       12        1        2
var date = new Date();
var currentMonth = date.getMonth()+1; // months go from 0 to 11, but your code is 1-12

// add the new ordering parameter
for (var x = 0; x < myArray.length; x++) {
    if (myArray[x].Month > currentMonth) {
        myArray[x].Order = myArray[x].Month - 12;
    } else {
        myArray[x].Order = myArray[x].Month;
    }
}

// sort the array using the new Order value
myArray.sort(function compare(a,b) {
              if (a.Order < b.Order)
                 return -1;
              else
                 return 1;
            });
因为该图表应该以与当前月份相反的顺序显示6个月的数据。我尝试过几件事,包括:

   Code Updated Below:
调用方法并用对象数组加载它:

<script>

monthArray = [];
        monthArray.push({"Month" : 1,
            "Count" : 1252});
        monthArray.push({"Month" : 2,
            "Count" : 1252});
        monthArray.push({"Month" : 9,
            "Count" : 1252});
        monthArray.push({"Month" : 10,
            "Count" : 1252});
        monthArray.push({"Month" : 11,
            "Count" : 1252});
        monthArray.push({"Month" : 12,
            "Count" : 1252});

sTest(monthArray);
</script>
现在,为了测试的目的,我在一个常规的html文件中完成了这一切。 如果没有好的方法可以做到这一点,我会尝试另一种方法,但老实说,我不知道任何其他方法

作为参考,此代码的第二部分必须保持原样,除非有人知道更好的方法将json对象加载到这种形式的数据中,基本上,这只是我正在做的转换json对象的模拟,以便在将月份和数据加载到我正在使用的d3.js条形图之前,我可以有效地重命名它们

提前谢谢

编辑:

我最近尝试编辑代码以连接两个数组并显示连接的数组,但仍然收到相同的结果。以下是新代码:

<script>
var sTest = function(array) {

    var newArray = [];
    var newArray2 = [];
    var currentMonth = 2;
    var index = -1;

    //loop through the array to grab the index of the current date to set 
    //my starting and ending points for loading the new array
    for (i = 0; i < array.length; i++) {
        document.write(array[i].Month + "<br/>");
        if (array[i].Month === currentMonth) {
            index = i;
        }
    }

    //test the index of the current date
    document.write("The index of the current date is: " + index + "<br/>");

    //test the length just to be sure and avoid out of bounds errors
    document.write("The length of the current array is: " + array.length + "<br/>");

    //Get the data after the current month and load it to the array first
    for (j = index + 1; j < array.length; j++) {
        newArray.push({"Month" : array[j].Month, "Count" : array[j].Count});
    }    

    //get the data up to the current month and load it into the new array last
    for (k = 0; k <= index; k++) {
        newArray2.push({"Month" : array[k].Month, "Count" : array[k].Count});
    }

    var finalArray = newArray.concat(newArray2);

    //test the printout of the new array to examine the results
    for (i = 0; i < finalArray.length; i++) {
        document.write("Month: " + array[i].Month + " Count: " + array[i].Count + "<br/>");
    }

};
</script>

查看JavaScript中的属性。如果使用日期数学,而不是整数数学,那么1/15将出现在12/14之后

如果您可以稍微更改阵列,以下是一个解决方案:

  • 在对象中添加名为Order的新参数
  • “修复”月份并将其分配给订单(因为当前月份将是最后一个月,任何具有更高值的月份都是上一年的月份,将其值减少12)
  • 按新的顺序值对数组进行排序
  • 代码如下所示:

    var myArray = [
               {Month : 1, Count : 1000},
               {Month : 2, Count : 1000},
               {Month : 9, Count : 1000},
               {Month : 10, Count : 1000},
               {Month : 11, Count : 1000},
               {Month : 12, Count : 1000},
         ]; 
    
    ________________________________________________
     09       10       11       12        1        2
    
    var date = new Date();
    var currentMonth = date.getMonth()+1; // months go from 0 to 11, but your code is 1-12
    
    // add the new ordering parameter
    for (var x = 0; x < myArray.length; x++) {
        if (myArray[x].Month > currentMonth) {
            myArray[x].Order = myArray[x].Month - 12;
        } else {
            myArray[x].Order = myArray[x].Month;
        }
    }
    
    // sort the array using the new Order value
    myArray.sort(function compare(a,b) {
                  if (a.Order < b.Order)
                     return -1;
                  else
                     return 1;
                });
    
    var-date=新日期();
    var currentMonth=date.getMonth()+1;//月份从0到11,但代码是1-12
    //添加新的排序参数
    对于(var x=0;xcurrentMonth){
    myArray[x]。订单=myArray[x]。第12个月;
    }否则{
    myArray[x]。订单=myArray[x]。月份;
    }
    }
    //使用新的顺序值对数组进行排序
    排序(函数比较(a,b){
    如果(a.订单
    您可以看到它在这里工作:


    这里有一个“更干净”的解决方案,它将所有这些合并到比较函数中,并且不需要额外的变量:

    // sort the array using the new Order value
    myArray.sort(function compare(a,b) {
              var date = new Date();
              var orderA = a.Month > date.getMonth()+1 ? a.Month -12 : a.Month;
              var orderB = b.Month > date.getMonth()+1 ? b.Month -12 : b.Month;
              if (orderA < orderB)
                 return -1;
              else
                 return 1;
            });
    
    //使用新的顺序值对数组进行排序
    排序(函数比较(a,b){
    变量日期=新日期();
    var orderA=a.Month>date.getMonth()+1?a.Month-12:a.Month;
    var orderB=b.Month>date.getMonth()+1?b.Month-12:b.Month;
    if(orderA

    看到它在这里工作:

    您的问题就在这里:

    //test the printout of the new array to examine the results
    for (i = 0; i < finalArray.length; i++) {
        document.write("Month: " + array[i].Month + " Count: " + array[i].Count + "<br/>");
    }
    
    //测试新数组的打印输出以检查结果
    对于(i=0;i);
    }
    

    您正在循环使用
    finalArray.length
    但您的document.write使用的是
    array[i]
    而不是
    newArray[i]
    -您正在循环使用原始未修改的数组。

    您的问题标题询问的是一个本身没有重新排序的数组(哪些数组无论如何都不会重新排序)但是您的问题看起来好像是您创建的数组没有重新排序(这是我所期望的)。现在我完全不知道你在问什么,因为标题和问题似乎是矛盾的。@StephenP我该如何澄清这个问题?我自己对此有点迷茫,我故意按数组中所需的顺序推送必要的数据,但仍然得到了与以前相同的结果。考虑到这一点,我还尝试创建两个单独的数组并将它们连接起来,但仍然没有得到预期的结果。@StephenP好的,我已经更改了问题标题,希望能少一点误导。我的问题是如何将月份整数以匹配最近六个月视图的形式放入数组中。我尝试了几种方法,但即使在故意将它们按特定顺序(9,10,11,12,1,2)放入数组后,数组仍然以数字顺序(1,2,9,10,11,12)出现,我也不明白为什么。很抱歉,使用日期这一术语有点含糊不清,我已经更新了这个问题,以类似于我实际正在做的事情。数组对象中的月份只是一个整数,我将在调用视图时将其转换为月份名称。我的问题是,每次我尝试对列表进行排序时,任何大于当前月份的内容都会以“最近六个月视图”的形式显示在当前月份的后面,但我似乎无法将其进行排序。这非常巧妙,非常巧妙。我真希望我能想到这样的事情,这太棒了!!!哈哈哈,该死的复制粘贴把我弄上了那个。我非常抱歉,这绝对是我忽略的错误,我不知道我在想什么,也不知道为什么我一直忽略它。我不应该如此无知,以为我可以通过复制粘贴而不出错。我真的得慢下来了。这是一个经典的“我很匆忙”,完全把我自己搞砸了。我不知道我在想什么!!!