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