executable
executable

Reputation: 3590

Count number of the same input value

I have these fallowing inputs :

<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="5">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">

How can I count the number of the same value ?

For example:

The value 23 appears 4 times

The value 23 appears 3 times

The value 5 appears once

The value 9 appears twice

I know I can loop using

$(".type_id").each(function() {
    console.log($(this).val())
});

Upvotes: 0

Views: 562

Answers (6)

Alireza Ahmadi
Alireza Ahmadi

Reputation: 9893

Try this one using Dictinary:

  var dictinary = []; // create an empty array

    $(".type_id").each(function () {
        var val = $(this).val();

        if (dictinary[val])
            dictinary[val]++;
        else
            dictinary[val] = 1;
    });

    console.log(dictinary)

Upvotes: 0

KooiInc
KooiInc

Reputation: 122888

A oneliner

const frequencies = [...document.querySelectorAll('.type_id')]
  .reduce( (acc, val) => 
    ({...acc, [val.value]: +(acc[val.value] || 0) + 1}), {});
    
console.log(frequencies);
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="5">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">

Upvotes: 0

Paul Zaslavskij
Paul Zaslavskij

Reputation: 654

Simply, you need to memoize all values, it's better to use object for it.

const inputs = document.getElementsByClassName('type_id')

const result = Array.from(inputs).reduce((acc, rec) => {
  const currentValue = rec.value
  if (typeof acc[currentValue] !== "undefined") {
    return {...acc, [currentValue]: acc[currentValue] + 1}
  }
  return {...acc, [currentValue]: 1}
}, {})

console.log(JSON.stringify(result, 2, 2))

Here my working is jsfiddle example: https://jsfiddle.net/ahs21myo/2/

Upvotes: 0

Yuriy Yakym
Yuriy Yakym

Reputation: 3911

const numbers = Array.from($('.type_id'), input => parseInt(input.value, 10));
const result = numbers.reduce((result, number) => {
  result[number] = result[number] || 0;
  result[number]++;
  return result;
}, {});
console.log(result);

or you can use lodash's countBy like so:

const numbers = Array.from($('.type_id'), input => parseInt(input.value, 10));
const result = countBy(numbers);

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122027

You can select inputs, create an array from that NodeList and apply reduce method to get count for each value as one object. you can do the same with jquery selector $('.type_id')

const inputs = document.querySelectorAll('.type_id')

const count = [...inputs].reduce((r, { value }) => {
  r[value] = (r[value] || 0) + 1
  return r;
}, {})

console.log(count)
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="5">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">

Upvotes: 1

ahsan
ahsan

Reputation: 1504

You can use a dictionary to count the number of times a value is repeated.

let inputs = document.querySelectorAll(".type_id");
let frequency = {};

for(let i=0; i<inputs.length; i++){
  if(frequency[inputs[i].value]){
    frequency[inputs[i].value]++;
  }else{
    frequency[inputs[i].value] = 1;
  }
}

console.log(frequency);
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="11">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="5">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="9">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="23">
<input type="hidden" class="type_id" value="11">

Upvotes: 2

Related Questions