HighDots Forums  

come si rileva un indice?

Javascript (Italian) Il linguaggio JavaScript (it.comp.lang.javascript)


Discuss come si rileva un indice? in the Javascript (Italian) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
motion musso aka: sathia
 
Posts: n/a

Default come si rileva un indice? - 08-12-2006 , 09:24 AM






ciao, devo fare un confronto tra due array di stessa dimensione,
uno è ordinato numericamente, l'altro ha i riferimenti ai nodi html che mi
servono.

purtroppo non ho trovato un modo per ottenere l'indice corrente durante il
for del primo array. esiste un metodo?

Grazie
--
una volta qui era tutto <table>

Reply With Quote
  #2  
Old   
raenkrus
 
Posts: n/a

Default Re: come si rileva un indice? - 08-14-2006 , 08:12 AM






motion musso aka: sathia ha scritto:

Quote:
ciao, devo fare un confronto tra due array di stessa dimensione,
uno è ordinato numericamente, l'altro ha i riferimenti ai nodi html chemi
servono.

purtroppo non ho trovato un modo per ottenere l'indice corrente durante il
for del primo array. esiste un metodo?

aspè Sathia vediamo se ho capito bene:
- tu cicli il primo array finché trovi l'indice che ti interessa;
- tale indice lo usi per estrarre dal secondo array il nodo html
corrispondente.
è così?

il primo for diventa (non testato!)

for (var i=0;i<primoArray.length;i++) {
if (primoArray[i] == 'quelchetiserve') {
var Indice = i;
break;
}
}

e il secondo for a questo punto non serve perché hai
l'indice che ti interessa e lo usi a tuo piacimento:

var nodoHTML = secondoArray[Indice];

se ho capito male chiedo venia in anticipo.

rae



Reply With Quote
  #3  
Old   
motion musso aka: sathia
 
Posts: n/a

Default Re: come si rileva un indice? - 08-14-2006 , 09:33 PM



raenkrus wrote:

ehi, ciao!

Quote:
aspè Sathia vediamo se ho capito bene:
- tu cicli il primo array finché trovi l'indice che ti interessa;
- tale indice lo usi per estrarre dal secondo array il nodo html
corrispondente.
è così?

esattamente!

Quote:
il primo for diventa (non testato!)

for (var i=0;i<primoArray.length;i++) {
if (primoArray[i] == 'quelchetiserve') {
var Indice = i;
break;
}
}
il problema è che a me non serve l'indice i, ma l'indice vecchio di
primoArray
ti posto in inglese la gentile risposta dal newsgroup internazionale:

motion musso aka: sathia wrote:
Quote:
Lasse Reichstein Nielsen wrote:


Index of what? If the variable "i" is 0, do you want the index
of "a" in myarray? If so, how do you find "a" from the value 0,
if you don't have the original value of myarray any more?


I got it now,
I thought that when you give indexes to an array they would remain
unvaried.
probably this is true only with string indexes.
Read the ECMAScript Language reference (section 15.4) - array indexes
*are* strings:

* "Array objects give special treatment to a certain class of
* *property names. A property name P (in the form of a string
* *value) is an array index if and only if ToString(ToUint32(P))
* *is equal to P and ToUint32(P) is not equal to 2^32-1."


I think you are getting confused between Object objects and Array
objects. *The only way to find the index of a particular value in an
array is to search for it, e.g.:

* var anArray = ['apple', 'banana', 'pear'];

What is the index of 'banana'?

* for (var i=0; i<anArray.length; i++){
* * if ('banana' == anArray[i]) {
* * * alert('banana is at index ' + i);
* * }
* }

Now if you do:

* anArray.shift();

What is the index of 'banana' now? *Arrays don't have to be contiguous:

* anArray[100] = 'orange';


The length of anArray is now 101, even though it only has 4 elements
(or items or members, whatever). *Such arrays are often called
'sparse'. *You can iterate over just the indexes with a value using
for..in, but be aware that will find *all* enumerable properties of the
Array, not just the ones with numeric indexes.

Secondly, remember that Array's are just objects with a few extra
methods and a special length property. *You can add properties to
ordinary objects and give them 'indexes', and you can add properties to
an array that don't have numeric indexes:

* anArray.name = 'fred';

Now a for..in loop through anArray will stumble across 'fred' as well
as the four fruits, but looping over the indexes won't.

Also think about:

* var anObject = { 0 : 'apple', 1 : 'banana', 2 : 'pear'};


How would you find the 'index' of banana now? *Probably using a for..in
loop:

* for (idx in anObject){
* * if ('banana' == anObject[idx]){
* * * alert('Property name for banana is ' + idx);
* * }
* }


I hope that helps.


Rob

--
una volta qui era tutto <table>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.