Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中对三维数组排序_Javascript_Arrays_Multidimensional Array - Fatal编程技术网

在Javascript中对三维数组排序

在Javascript中对三维数组排序,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,考虑这个数组 var LIST =[]; LIST['C']=[]; LIST['B']=[]; LIST['C']['cc']=[]; LIST['B']['bb']=[]; LIST['C']['cc'].push('cc0'); LIST['C']['cc'].push('cc1'); LIST['C']['cc'].push('cc2'); LIST['B']['bb'].push('bb0'); LIST['B']['bb'].push('bb1'); LIST['B']['

考虑这个数组

var LIST =[];

LIST['C']=[];
LIST['B']=[];

LIST['C']['cc']=[];
LIST['B']['bb']=[];  

LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');
我可以像这样循环这个数组

  for(var i in LIST){

      console.log(i)//C,B
      var level1=LIST[i];

      for(var j in level1){
        console.log(j)//cc,bb
        // etc...
      }

   }
好。。我有几个基本问题

1.如何在每个级别对数组进行排序?

一个级别可以是sort by.sort(fn)方法。我如何才能传递到内部级别

2.为什么
indexOf
方法无法找到前两个级别的元素?

若这是因为一个非字符串参数。。如果项目不是字符串,如何搜索数组中的项目

3.for(列表中的var i)的
是如何工作的?
我只需要对数组的索引和循环有一个基本的了解


谢谢。

您的排序标准是什么?我的意思是,你怎么说array
firstArray
secondArray
之前? 关于(myArray中的计数器)的
计数器将在每次迭代中获取数组元素的值

对于(计数器位于[0,1,5])
,计数器将在3次迭代中具有值0、1和5

在您的情况下,
i
将在两次迭代中具有值
LIST['B']
LIST['C']
,而
j
将具有值
LIST['B']['bb']、LIST['B']['cc']、LIST['C']['bb']
LIST['C']['cc']


i
j
都将是数组。

您需要知道
Array
继承自
对象

在JavaScript中,任何
对象
实例都是一个关联数组(!),因此其行为类似于PHP中的数组。例如:

var o = {}; // or new Object();
o['foo'] = 'bar';
o[0] = 'baz';
for (i in o) { console.log(i, o[i]); }
对象进行排序
没有多大意义
indexOf
在理论上是可行的,但尚未实现

数组
是有序列表
Array
实例有
push()
length
indexOf()
sort()
等,但这些只适用于数字索引。但同样,
数组
继承自
对象
,因此任何数组也可以包含非数字索引项:

var a = []; // or new Array();
a[0] = 'foo'; // a.length is now 1
a.push('baz'); // a[1] === 'baz'
a.qux = 1; // will not affect a.length
a.sort(); // will not affect a.qux
for (i in a) { console.log(i, a[i]); }

我建议你玩转数组和对象,你很快就会明白这一点。

LIST
在Javascript中不是一个三维数组,它只是一个数组

//declare an array which names LIST.
var LIST = [];

//set a property named 'C' of the LIST to be an array.
LIST['C']=[];
//set a property named 'B' of the LIST to be an array.
LIST['B']=[];

//set a property named 'cc' of the 'LIST.C'(which is an array object)
LIST['C']['cc']=[];
//set a property named 'bb' of the 'LIST.B'(which is an array object)
LIST['B']['bb']=[];  
事实上,您只需要将最后一级设置为数组,请参见下面的示例代码

function iterateOrderd(obj) {
    if (obj instanceof Array) {
        obj.sort();
        for (var j = 0, l=obj.length; j < l; j++) {
            console.log(obj[j]);
        }
    } else {
        var sortable = [];
        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                sortable.push(i);
            }
        }
        sortable.sort();
        for (var j = 0, l=sortable.length; j < l; j++) {
            console.log(sortable[j]);
            iterateOrderd(obj[sortable[j]]);
        }
    }
}


var LIST = {};

LIST['C'] = {};
LIST['B'] = {};

LIST['C']['cc']=[];
LIST['B']['bb']=[];  

LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');

iterateOrderd(LIST);
函数迭代顺序(obj){
if(阵列的obj实例){
obj.sort();
对于(var j=0,l=obj.length;j
大多数代码都应该使用对象文本
{}
。数组(
[]
)应该具有递增的数字键值。@ZZBOV是正确的-您的对象是数组实例,但您没有正确使用它们。要对数组进行排序,请使用回调函数的方法。我的排序标准仅基于字母顺序,实际上,元素将以随机顺序添加..在循环时,我将以相同的顺序获得..好。。谢谢所以这里我试着按字母顺序对属性进行排序。。或者在循环的过程中,我需要按字母顺序得到它。。如何?是否可以根据非数字索引项按字母顺序对其进行排序。。