www.webdeveloper.com
Results 1 to 4 of 4

Thread: Array philosophical question

  1. #1
    Join Date
    Oct 2008
    Posts
    6

    Array philosophical question

    Hi this is my question:

    Can an Array be equal to a function that returns an Array and receives an Array as argument, been this Array the same original Array.

    Please take a look at my code.
    I need to filter an Array of Arrays twice to get an Array with a single Array, based on the values that are different.

    To accomplish that I have create a filter function that receives 3 arguments, the array(which is actually an Object), the argument(which is a property of the Object), and the value (of the property) Im looking for.

    The funny thing is that in IE and FF it is not working, but in Safari it only works the first time.

    My code looks like this.

    // I create this array beacuse I dont whant to destroy the original
    var mod = Array();

    alert("Modelos vacio:" + mod.length); //returns 0

    for (i=0; i < supermodelos[$("#modelo").val()].modelos.length; i++){
    mod.push(supermodelos[$("#modelo").val()].modelos[i]);
    }

    // alert returns 6 for yaris
    alert("Modelos lleno:" + mod.length);

    mod = removeDifferent(mod, cilindrada, cil);
    // breaks in FF y IE, should returns 3 for yaris 1.3

    alert("Modelos misma cilindrada:" + mod.length);
    mod = removeDifferent(mod, transmision, tran);

    // breaks in Safari, should returns 1 for yaris 1.3 automatico
    alert("Modelos misma transmision:" + mod.length);

    function removeDifferent(array, argumento, valor) {
    for(var i = 0; i < array.length; i++) {
    if(array[i].argumento != valor) {
    array.splice(i,1);
    }
    }
    return array;
    }

    full code can be seen in

    http://www.libelula.com.pe/toyota/
    http://www.libelula.com.pe/toyota/js/modelos.js

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Are you sure you need a pure javascript advice? Javascript has no native $() method. I suspect you are using a javascript framework, but we can not be sure what the custom method/function $() returns, unless you tell us.

    If $() returns a DOM element, that means you are dealing with a collection of elements (which is NOT quite the same thing as an array!), and this kind of collection does not support all the arrays native methods. push() is one of them.

    The common way to add dynamically new members in an array or in a collection is to use the array's/collection's length:
    Code:
    var mod = []; 
    for (var i=0; i < supermodelos[$("#modelo").val()].modelos.length; i++){
    mod[mod.length]=supermodelos[$("#modelo").val()].modelos[i];
    }
    Last edited by Kor; 12-12-2008 at 04:53 AM.

  3. #3
    Join Date
    Oct 2008
    Posts
    6

    I'm using jQuery

    Hi Kor, thanks for your help

    I'm using jQuery. I'm using $() to get values of input form fields to check if they are egual to some properties of supermodelo and modelo which are objects like this

    Code:
    function SuperModelo() {
    	this.nombre = '';	
    	this.modelos = Array();
    }
    
    function Modelo() {
    	this.nombre = '';	
    	this.cilindrada = '';
    	this.transmision = '';
    	this.combustible = '';
    	this.traccion = '';
    	this.cabina = '';
    	this.consumo = '';
    	this.factor = '';
    }
    
    var supermodelos = Array();
    var sm=null;
    var md=null;
    
    /* * * * M OD E L O S * * * */
    
    /* * * * AURIS * * * */
    sm = new SuperModelo;
    sm.nombre='Auris';
    
    md = new Modelo;
    md.nombre = 'AURIS 1.6 M/T';	
    md.cilindrada = '1.6';
    md.transmision = 'M';
    md.combustible = 'Gasolina';
    md.traccion = '';
    md.cabina = '';
    md.consumo = '50';
    md.factor = '8.16';
    sm.modelos.push(md);
    
    md = new Modelo;
    md.nombre = '';	
    md.cilindrada = '';
    md.transmision = '';
    md.combustible = 'Gasolina';
    md.traccion = '';
    md.cabina = '';
    md.consumo = '50';
    md.factor = '8.16';
    sm.modelos.push(md);
    
    supermodelos.push(sm);
    My spaghetti is much longer than that. Firts 780 lines are "modelos"

    Also I have found an error in my code because I was using DOT notation where I shoud use ARRAY notation in my function. It should look like this

    Code:
    function removeDifferent(array, argumento, valor) {
        for(var i = 0; i < array.length; i++) {
            if(array[i][argumento] != valor) { //here was my mistake
                array.splice(i,1);
            }
        }
        return array;
    }
    Even so it is not working.

    Please help


    Carlos

  4. #4
    Join Date
    Oct 2008
    Posts
    6
    I find out that I also missed some quotes

    Code:
    mod = removeDifferent(mod, "cilindrada", cili);
    			
    mod = removeDifferent(mod, "transmision", tran);
    and now it works fine. Thanks for your help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles