/    Sign up×
Community /Pin to ProfileBookmark

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

[url]http://www.libelula.com.pe/toyota/[/url]
[url]http://www.libelula.com.pe/toyota/js/modelos.js[/url]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 12.2008 — 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 ([I]which is NOT quite the same thing as an array![/I]), and this kind of collection does not support all the arrays native methods. [B]push()[/B] 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:
<i>
</i>var mod = [COLOR="Blue"][][/COLOR];
for ([COLOR="Blue"]var[/COLOR] i=0; i &lt; supermodelos[$("#modelo").val()].modelos.length; i++){
[COLOR="Blue"]mod[mod.length][/COLOR]=supermodelos[$("#modelo").val()].modelos[i];
}
Copy linkTweet thisAlerts:
@chiste_gmail_coauthorDec 12.2008 — 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);
[/CODE]


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;
}
[/CODE]


Even so it is not working.

Please help


Carlos
Copy linkTweet thisAlerts:
@chiste_gmail_coauthorDec 12.2008 — I find out that I also missed some quotes

[CODE]
mod = removeDifferent(mod, "cilindrada", cili);

mod = removeDifferent(mod, "transmision", tran);

[/CODE]


and now it works fine. Thanks for your help.
×

Success!

Help @chiste_gmail_co spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...