2

Consider this 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']['bb'].push('bb2');

I can loop through this array like

  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...
      }

   }

Fine.. I have few basic questions.

1.How to sort the array in each level?

One level can be sort by .sort(fn) method . How can i pass to inner levels?

2.Why the indexOf method does not works to find the elements in first two levels?

If it's because of the a non string parameter .. how can i search an array items in array if the item is not string?

3.How for(var i in LIST) works ? I just need a basic understanding of indexing and looping through array ..

Thanks ..

3
  • 5
    You should be using the object literal {} for most of your code. Arrays ([]) should have incremental numeric key values. Commented Jan 23, 2012 at 15:33
  • @zzzzBov is correct - your objects are Array instances but you're not using them properly. Commented Jan 23, 2012 at 15:35
  • To sort an array use Array.sort method with callback function. Commented Jan 23, 2012 at 15:42

3 Answers 3

1

LIST is NOT a three dimensional array in Javascript, it is just an array.

//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']=[];  

The fact is you only need to let the last level to be an array, see my example code below.

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);
Sign up to request clarification or add additional context in comments.

1 Comment

ok.. thanks .. so here im trying to sort the properties in alphabetical order.. or while looping through that i need to get that in alphabetical order.. how?
1

You need to know that Array inherits from Object.

In JavaScript, any Object instance is an associative array(!), so acts like an Array in PHP. For example:

var o = {}; // or new Object();
o['foo'] = 'bar';
o[0] = 'baz';
for (i in o) { console.log(i, o[i]); }

Sorting an Object does not make much sense. indexOf would kinda work in theory, but is not implemented.

Arrays are ordered lists. Array instances have push(), length, indexOf(), sort() etc., but those only work for numerical indexes. But again, Array inherits from Object, so any array can also contain non-numerical index entries:

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]); }

I recommend playing around with arrays and objects, and you'll soon get the point.

1 Comment

is it possible to sort it based on non-numerical index entries in alphabetical order..
0

What is your sorting criteria ? I mean how will you say array firstArray comes before secondArray? regarding the for (counter in myArray), counter will take values of an array element in every iteration.

for (counter in [0,1,5]), counter will have values 0, 1 and 5 in the 3 iterations.

In your case, i will have values LIST['B'] and LIST['C'] in the two iterations and j will have values LIST['B']['bb'], LIST['B']['cc'], LIST['C']['bb'] and LIST['C']['cc'].

Both i and j will be arrays.

1 Comment

my sorting criteria is just based on the alphabetical order, actually the elements will add in random order..while looping through it I will get in the same order..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.