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