...

View Full Version : Get arrays less than array number



gani
05-16-2011, 12:50 PM
I have 10 000 arrays in various array numbers like this

myArray[0] = "Football";
myArray[3] = "Baseball";
myArray[5] = "Cricket";
myArray[9] = "Football";
myArray[17] = "Baseball";
myArray[2289] = "Cricket";
myArray[739] = "Football";
myArray[235] = "Baseball";
myArray[13] = "Cricket";

How can I find out if there is any array less than myArray[500]? :confused:

And if i can find it out ... can I automatically list all those arrays in another array for example? Because I want to delete all arrays less than myArray[500] without having to loop. because when I have a million arrays .. it will take too long. :(

bullant
05-16-2011, 01:01 PM
How can I find out if there is any array less than myArray[500]? :confused:

And if i can find it out ... can I automatically list all those arrays in another array for example?

Maybe use this demo as a guide


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
<script type="text/javascript">
var outArray = new Array();
var myArray = new Array();
myArray[0] = "Football";
myArray[3] = "Baseball";
myArray[5] = "Cricket";
myArray[9] = "Football";
myArray[17] = "Baseball";
myArray[2289] = "Cricket";
myArray[739] = "Football";
myArray[235] = "Baseball";
myArray[13] = "Cricket";
for(var i in myArray){
if(i < 500){outArray.push(myArray[i]);}
}
//for testing purposes output outArray
var str = '';
for(i=0; i < outArray.length; i++){
str += outArray[i]+"\n";
}
alert(str);
</script>
</head>
<body>

</body>
</html>

gani
05-16-2011, 01:37 PM
looks like what i need. However, excuse the ignorance but doesn't


for(var i in myArray)

loop through all the arrays elements?

if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer. :(

oVTech
05-16-2011, 01:43 PM
looks like what i need. However, excuse the ignorance but doesn't


for(var i in myArray)

loop through all the arrays elements?

if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer. :(


I don't think that people do that kind of thing on the client. Maybe you should look into PHP and AJAX for dealing with a million array items or arrays.

Kor
05-16-2011, 01:59 PM
looks like what i need. However, excuse the ignorance but doesn't


for(var i in myArray)

loop through all the arrays elements?

if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer. :(
You make a confusion between Arrays and Objects.

An Array is an ordered list of values. Circling through the elements of an array is to be made using an incrementing loop, a simple for or an while loop

An Object is an unordered list of property:value pairs. Circling through the properties of an Object is to be made using a for/in loop.



var myArray=[ // this is an Array
'John Doe',
34
]
for(var i=0;i<myArray.length;i++){
alert(myArray[i]);
}
var myObject={ // this is an Object
'name':'John Doe',
'age':34
}
for(property in myObject){
alert(property+' : '+myObject[property])
}

And, if there are 1.000.000 or elements, that means you should reconsider the way you input the data. 1 million is too much even for a server-side language or an ordinary DBMS.

gani
05-16-2011, 01:59 PM
unfortunately it must be on clientside

So you think there is no way to do something like


Get all myArray where arrayElementNumber < 500

:confused:

gani
05-16-2011, 02:09 PM
ok then ...I might as well explain what I am trying to do... :rolleyes:

I am dynamically creating and deleting many divs on a website. Each div has a separate id starting from 1 to 1000 000. Of course, I will not have a million div on the site. i will only have 100 or so at a time.... The problem is, the user is the one who decides which div, from 1 to 1000 000 is going to appear on the screen. So they can randomly pick 100 divs. Before I create those, i have to delete all divs that were there before.

:rolleyes:

any ideas ?

Kor
05-16-2011, 02:10 PM
Simply set an upper limit of the loop increment


var limit=500;
for(var i=0;i<limit;i++){
// do whichever with myArray[i]
}

Kor
05-16-2011, 02:13 PM
ok then ...I might as well explain what I am trying to do... :rolleyes:

I am dynamically creating and deleting many divs on a website. Each div has a separate id starting from 1 to 1000 000. Of course, I will not have a million div on the site. i will only have 100 or so at a time.... The problem is, the user is the one who decides which div, from 1 to 1000 000 is going to appear on the screen. So they can randomly pick 100 divs. Before I create those, i have to delete all divs that were there before.

:rolleyes:

any ideas ?
As I said 1000000 in a row is much too much. Find a way to split, organize and systematize data.

bullant
05-16-2011, 02:19 PM
looks like what i need. However, excuse the ignorance but doesn't


for(var i in myArray)loop through all the arrays elements?

if so, then it is not good because if there are 1000 000 elements it will be too much on the client computer. :(

My understanding of the the way the loop works is that it only loops through the actual elements in the array. So even if there are potentially 1,000,000 elements but only 9 actually exist as in your code then only 9 elements are looped through and the ones that meet the test condition in the IF statement are then pushed to the output array.

Kor
05-16-2011, 02:30 PM
OK, if you do not really have 1.000.000, but you want to use numbers from 0 to 1.000.000, you should definitely use an Object, not an Array. As bullant said, if you define just a single array's element as myArray[999999], myArray will still have 1000000 of elements, from which 999999 will be null.


var myObject= {
'0':"Football",
'3':"Baseball",
'5':"Cricket",
'9':"Football",
'17':"Baseball" // and so on - note: the last elements must not have a comma delimiter, IE<9 fix
}

gani
05-16-2011, 02:37 PM
oh... i did not know that .... I thought I could just say myArray[999999] and have just one element in the array :eek:

So I guess I am back to square one then.... I need to use an object....
But now how will i find the records less than 500 using objects .... :confused:

Last question guys ...promise :D

Kor
05-16-2011, 02:39 PM
On the other hand, if your array given as example is real, why don't you write the array in another way?


var myArray=[
['Footbal',[0,9,739]],
['Baseball',[3,17,235]],
['Cricket',[5,2289,13]]
]


Still I guess that an Object is the proper solution...

Kor
05-16-2011, 02:47 PM
But now how will i find the records less than 500 using objects .... :confused:



var myObject= {
'0':"Football",
'3':"Baseball",
'5':"Cricket",
'9':"Football",
'17':"Baseball",
'2289':"Cricket",
'739':"Football",
'235':"Baseball",
'13':"Cricket"
}
var limit=500;
var outObject={};
var a;
for(a in myObject){
Number(a)>limit?outObject[a]=myObject[a]:null;
}
//test:
for(a in outObject){
alert(a+' : '+outObject[a]);
}

gani
05-16-2011, 02:59 PM
thanks a million .. just what I need :thumbsup:

Much appreciated ;)

gani
05-16-2011, 03:38 PM
Stuck again :D



personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";
the above creates this...

personObj={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};


How can I rewrite the code so that the identifiers are number strings... like this

personObj={'1':"John",'2':"Doe",'3':50,'4':"blue"};

Kor
05-16-2011, 03:44 PM
use the square bracket notation:


personObj['1']='John';
// and so on

It is essential to keep/define the properties of an object within quotes (as strings), especially when they are "number-like".

rnd me
05-16-2011, 07:27 PM
As bullant said, if you define just a single array's element as myArray[999999], myArray will still have 1000000 of elements, from which 999999 will be null.

they will be undefined, not null.

rnd me
05-16-2011, 07:28 PM
It is essential to keep/define the properties of an object within quotes (as strings), especially when they are "number-like".

why? numbers work for me, what's the extra conversion for?

Kor
05-16-2011, 07:30 PM
they will be undefined, not null.
Correct, as they are not objects. null is an object. I was using the null term in a common language way, to emphasize something else (the length of the Array), not as a JavaScript term.

Kor
05-16-2011, 07:35 PM
why? numbers work for me, what's the extra conversion for?

To avoid problems. That's why:


var myObject={
0:'one',
1:'two'
}
alert(myObject.0)

Compare with:


alert(myObject['0'])

rnd me
05-16-2011, 09:14 PM
To avoid problems. That's why:


var myObject={
0:'one',
1:'two'
}
alert(myObject.0)


Compare with:


alert(myObject['0'])


i mean why


alert(myObject['0'])


instead of


alert(myObject[0])


seems like the quot/apos is not needed for integer key names since the string version of 0 is "0"...

gani
05-17-2011, 01:39 AM
again thank you all. I am a happy man :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum