count array skipping the unique values [duplicate] - javascript

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 8 years ago.
I'm like to count the total values in my array but I like to skip the same value
my array example, my real array will have about 1000 values.
json2=[aaa,aaa,aaa,aaa,bbb,bbb,bbb,ccc,ccc,ccc,ccc,ccc,ddd,ddd,ddd,eee,eee,fff];
and i want my count result to be
var countBoxID=6;
i only got
for(i in json2){
countBOXID ++
}

You can make use of this function:
function GetUnique(inputArray)
{
var outputArray = [];
for (var i = 0; i < inputArray.length; i++)
{
if ((jQuery.inArray(inputArray[i], outputArray)) == -1)
{
outputArray.push(inputArray[i]);
}
}
return outputArray;
}

Related

Can anyone explain me how this possible in javascript? [duplicate]

This question already has answers here:
JavaScript variable number of arguments to function
(12 answers)
Closed 11 months ago.
let sum = 0;
function innerAdd(a,b){
sum = a + b;
console.log(sum);
}
innerAdd(5,5,25,25) // Still getting 10 as output
Javascript only recognizes the first two values. if you want to be able to add more values you will have to add a for() cycle. something like this:
function sum() {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
total += arguments[i];
}
alert(total);
}
sum(1,2,3,4);
this way you can add any number of parameters, sum(5,5,25,25) will be 60.

Print all the names of keys stored in localStorage html [duplicate]

This question already has answers here:
Get HTML5 localStorage keys
(15 answers)
Closed 7 years ago.
I'm trying to print the names of all the keys stored in localStorage with each key in a separate line. This is my code:
function viewsaved(){
$('#saved').show();
var stuffsaved = Object.keys(localStorage);
var splitit = stuffsaved.split(',');
for (var i = 0 ; i < splitit.length ; i++ ){
$('#saved').append(splitit[i]+"<br>");
}
}
when I call the function, it does nothing.
How do you do this properly?
Object.keys returns an array, not a string. Just modify slightly:
var stuffsaved = Object.keys(localStorage);
for (var i = 0 ; i < stuffsaved.length ; i++ ) {
$('#saved').append(stuffsaved[i]+"<br>");
}
If you have or expect a lot of keys, I would suggest building the list in a temporary variable first to avoid frequent DOM update, for example:
var keys = Object.keys(localStorage);
var list = "";
for (var i = 0 ; i < keys.length ; i++ ) {
list += keys[i] + "<br>";
}
$('#saved').append(list);

Generate 10 Unique Random Numbers between 1 to 10 in javascript [duplicate]

This question already has answers here:
How can I shuffle an array? [duplicate]
(2 answers)
Closed 8 years ago.
My exact requirement is to return all numbers from 1 to 2000 randomly and shouldn't repeat a number twice. So we can say if i have
function generateRnNumber(){
var arr = [1,2,3,4,5,6,7,8,9,10,...2000];
randomNumber = Math.floor((Math.random()*2000));
return randomNUmber;
}
So if i call generateRnNumber a number between 1 - 2000 which is not returned before, or a unique number so if i call 2000 times i should get all the numbers but in random order. I don't want to keep an array with 2000 elements. Please help me in writing this function.
Keep an array with all possible values and randomize the order. When you need a random value, pop the array (removing the value the "possible values"-array).
To do this otherwise, you would anyway need an array to hold the "taken" values so i don't think you can get around keeping an array of the size of your "value range".
You can shuffle the array like this:
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
usage:
var myArray = [];
for(var i = 1; i <= 2000; i++) {
myArray.push(i);
}
var randomArray = shuffle(myArray).splice(0, 10); // an array with 10 random numbers varrying from 1-2000
source: How can I shuffle an array?

Object's push method not working properly [duplicate]

This question already has answers here:
Javascript Object push() function
(10 answers)
Closed 8 years ago.
I have a object for which i am using push method and it gives me length one more than expected.
var Object=[];
var temp = {};
var j=0;
while(j<2){
temp.id= j+1;
//other properties setting
Object.push(temp);
j++;
}
console.log(Object.length);
gives me 3. Also I see three object values, first as empty second has id =1 and third has id =2.
.push is a function of Array and not Object and don't use variable names that cause ambiguity.
Use:
var arr = [];
var temp = {};
var j = 0;
while (j < 2) {
temp.id = j + 1;
//other properties setting
arr.push(temp);
j++;
}
console.log(arr.length);

grouping elements dynamically inside arrays [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 8 years ago.
I am new to javascript. I have a list of 500 or more items. I need split this array into sub arrays each containing 20 items. i.e 25 arrays if there are exactly 500 items and each array containing 20 items. I created 25 arrays like below:
var firstSet=[];
var secondSet=[];
.....
And i populate each of this array using the for loop. In javascript how can I make it programmatically since the main list can return more than 500 items in future and each sub array should be configured for more than 20 items in future. What is the best solution to fix this situation?
As comments say, you should split it with a 2 dimentional array:
var mainArray=[1,2,3,4,5,6,7,8,9,10];
function splitArray(arr,qty){
var mainArr=[], subarr=[];
for(var i=0;i<arr.length;i++){
subarr.push(arr[i]);
if( ((i+1) % qty == 0) || i+1==arr.length){
mainArr.push(subarr);
subarr=[];
}
}
return mainArr;
}
console.log(splitArray(mainArray,2));
This creates 1 array with 5 indexes. In each index, you have an array of 2 elements. So it groups it in [1,2], [3,4], [5,6], [7,8], [9,10]
Use nested loops to create a 2-dimensional array from the original data.
var sets = [];
var items_per_set = 20;
for (var i = 0, outer = 0; i < list.length; i += items_per_set, outer++) {
sets[outer] = [];
var limit = Math.min(list.length, i+items_per_set);
for (j = i; j < limit; j++) {
sets[outer].push(list[j]);
}
}

Categories