Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2d arrays! help 3

Status
Not open for further replies.

codegrl

Programmer
Jan 13, 2008
6
US
Hello, I am new to this forum but maybe you guys can help me


I have an array:
arrNums = 122379, 198702, 102884, 109231;

now this arrNums resets itself for every post on the page (there might be 4-5 posts on every page) and there might be repeating numbers in each of these posts. I need to write a 2d array myArray['num'] storing numbers and myArray['count'] storing count of each time the number has occured.

arrNums = 122379, 198702, 102884, 109231;

for (i=0; i<arrNums.length; i++){
for (j=0; j<myArray['num'].length; j++){
if (myArray['num'][j] == arrNums) // if this number already exists then increment count
myArray['num'] = arrNums; // otherwise set the new number


}


I know I am doing it completely wrong but i desperately need help! : )
 
By 'posts' do you mean http posts back to a server or comments that users might have made such as this one?

From what I can understand you want to take a single dimensioned array of numbers and convert it into an array of two-element arrays where the first element is always the number from the first array and the second is the number of times that it appears in the first array.

Code:
var oldArray = [1,2,3,4,5,6,7,8,9,1,3,5,7,9,3,6,9];
var newArray = new Array();
for(var i = 0; i < oldArray.length; i++){
 var numFound = false;
 for(var j = 0; j < newArray.length; j++){
  if(newArray[j][0] == oldArray[i]){
   numFound = true;
   newArray[j][1]++;
   break;
  }
 }
 if(!numFound){
  newArray[newArray.length] = [oldArray[i], 1];
 }
}
alert("Counted Array:\n" + newArray);


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
How about using objects something like this:
Code:
arrNums = [122379, 198702, 102884, 109231]; 
numberObj = new Array();
numberObj[0] = {number: arrNums[0], count: 1};

for (i=0; i<arrNums.length; i++)
  {
  var duplicate = false;
  for (j=0; j<numObj.length; j++)
    {
    if (numObj[j].number == arrNums[i])
      {
      duplicate = true;
      break;
      }
    }
  if (duplicate == true)
    {
    numObj[j].count++;
    }
  else
    {
    j = numOjb.length;
    numObj[j] = {number: arrNums[i]; count: 1};
    }
  }
}

Lee
 
Lee,
you wrote numberObj and later in script numObj.
justa a notice..

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
wow Brilliant. Thank you guys!
dwarfthrower, thats exactly what i needed but couldnt write the code myself.

trollacious, I want to start using the oo approach more but its harder to understand for me. So you are assigning properties such as count to this numberObj[0] and giving it the value of the arrNums[0] right? and then you are looping through this objects elements and comparing it to arrNums elements to see for duplicates?

dwarfthrower, what if I have lets say 5 articles on the page and each of those articles have an oldArry. so the value of the oldArry resets itself 5 times on a page and I still wanna be able to get all of the numbers for every occurance and assign them to the newArry. Will this method still work if run this after every occurance of the oldArry or will it overwrite stuff?
 
I just tested it and it works if the value of the oldArry is reset again and again.
 
Yes, you understand what I did correctly. First you assign the first element in arrNums to an object (created on the fly) that has 2 properties number and count. The object's number property is the number, and the count is 1.

Then the loop goes through the array of numbers and checks the array of existing objects for that number. If it exists, the variable duplicate is changed to true and you exit the loop. If the loop completes without finding the number in one of the objects, a new object is created in the array and that number added to it, with the count set to 1.

Lowet, thanks for catching that error. I was coming up with the code on the fly and didn't check it that well, as you noticed. Sometimes you gotta leave something for the person who asked the question to take care of. :)#

Lee
 
Hehe, it's easy to make such errors when you try to write a script fram scratch as fast as possible :p

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Thanks Lee, i understand better now and will play with this! : )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top