JS implements the number of occurrences of each element in the array

Source: Internet
Author: User

One,var arrayobj = [1, 1, 2, 3, 3, 3, 4, 5, 5];

To find out the number of occurrences of each element in the array, I'll use a for loop to do it briefly here. first declare a few variables,

var temp = "";//Intermediate variable, which is used to store an element of an array in a temporary storage loop

var count = The number of occurrences of an element in an array that is used to store an 0;//loop

var arrnew = new Array (); Create a new data to hold the data after the loop

  

Start the loop below

for (Var i=0;i<arrayobj.length;i++) {

if (arrayobj[i]!=-1) {

Temp=arrayobj[i];

for (Var j=0;j=arrayobj.length;j++) {//loops the array again, using the first loop to get the temp compared to each element of the array

if (Temp==arrayobj[j]) {

count++;//If two elements are the same, count is added once

ARRAYOBJ[J] = -1;//This time temporarily changes the value of the element variable in this loop so that it does not participate in the next loop

}

}

Arrnew.push (temp + ":" + count);//Save the elements of this loop and the number of occurrences to the new array

Count = 0;//to have the value of Count re-equals 0

}

}

  

Looping through a new array

for (Var i=0;i<arrynew.length;i++) {

Alert (Arrynew[i]);

}

  

Second, for such an array

var arr = [

{"Name": "Tom", "Age": "Ten", "gender": "Male"},

{"Name": "Lucy", "Age": "Ten", "gender": "Female"},

{"Name": "Tony", "Age": "One", "gender": "Male"},

{"Name": "Jack", "Age": "One", "gender": "Male"},

{"Name": "Yuanyuan", "Age": "A", "gender": "Female"}

];

The implementation of the same method as above is just one more step, this we find according to the age of how many, preferred we first find out the age is stored in an array.

var arrayobj=new Array ();

for (Var i=0;i<array.length;i++) {

Arrayobj.push (Arr[i].age);

}

The next step is the same as the above method, which is no longer stated here.

This is just one of my friends asked me a question, did a bit, perhaps there are better methods and logic to achieve.

JS implements the number of occurrences of each element in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.