Reputation: 8902
I would like to know if there is a native javascript code that does the same thing as this:
function f(array,value){
var n = 0;
for(i = 0; i < array.length; i++){
if(array[i] == value){n++}
}
return n;
}
Upvotes: 57
Views: 165249
Reputation: 491
Another option is to use Array.filter()
:
count = myArray.filter(x => x === searchValue).length;
Upvotes: 49
Reputation: 1
var obj = [];
var array = ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'd'];
array.forEach(element => {
let count = 1;
array.forEach(loopo => {
if(element == loopo)
{
obj[loopo] = count++;
}
})
});
console.log(obj);
Upvotes: -1
Reputation: 92894
There might be different approaches for such purpose.
And your approach with for
loop is obviously not misplaced(except that it looks redundantly by amount of code).
Here are some additional approaches to get the occurrence of a certain value in array:
Using Array.forEach
method:
var arr = [2, 3, 1, 3, 4, 5, 3, 1];
function getOccurrence(array, value) {
var count = 0;
array.forEach((v) => (v === value && count++));
return count;
}
console.log(getOccurrence(arr, 1)); // 2
console.log(getOccurrence(arr, 3)); // 3
Using Array.filter
method:
function getOccurrence(array, value) {
return array.filter((v) => (v === value)).length;
}
console.log(getOccurrence(arr, 1)); // 2
console.log(getOccurrence(arr, 3)); // 3
Upvotes: 84
Reputation: 11
This is how I did mine using just a for loop. It's not as sophisticated as some of the answers above but it worked for me.
function getNumOfTimes(arrayOfNums){
let found = {}
for (let i = 0; i < arrayOfNums.length; i++) {
let keys = arrayOfNums[i].toString()
found[keys] = ++found[arrayOfNums[i]] || 1
}
return found
}
getNumOfTimes([1, 4, 4, 4, 5, 3, 3, 3])
// { '1': 1, '3': 3, '4': 3, '5': 1 }
Upvotes: 1
Reputation: 41
You could use the Array filter method and find the length of the new array like this
const count = (arr, value) => arr.filter(val => val === value).length
Upvotes: 3
Reputation: 1
let countValue = 0;
Array.forEach((word) => {
if (word === searchValue) {
return countValue ++;
}
});
console.log(`The number of word 'asdf': ${countValue}`);
I used this code to count the number of a given word in a text which was previously converted into an array.
Upvotes: 0
Reputation: 498
const arr = ["a", "a", "a", "b", "b", "b", "b", "c", "c", "c"];
let count = 0;
function countValues(array, countItem) {
array.forEach(itm => {
if (itm == countItem) count++;
});
console.log(`${countItem} ${count}`);
}
countValues(arr, "c");
Upvotes: 2
Reputation: 21
let ar = [2,2,3,1,4,9,5,2,1,3,4,4,8,5];
const countSameNumber = (ar: any, findNumber: any) => {
let count = 0;
ar.map((value: any) => {
if(value === findNumber) {
count = count + 1;
}
})
return count;
}
let set = new Set(ar);
for (let entry of set) {
console.log(entry+":", countSameNumber(ar, entry));
}
Upvotes: 2
Reputation: 169
Here is my solution without using an additional object and using reduce
:
const occurrencesOf = (number,numbers) => numbers.reduce((counter, currentNumber)=> (number === currentNumber ? counter+1 : counter),0);
occurrencesOf(1, [1,2,3,4,5,1,1]) // returns 3
occurrencesOf(6, [1,2,3,4,5,1,1]) // returns 0
occurrencesOf(5, [1,2,3,4,5,1,1]) // returns 1
Upvotes: 9
Reputation: 99
You can also use forEach
let countObj = {};
let arr = [1,2,3,1,2,3,4];
let countFunc = keys => {
countObj[keys] = ++countObj[keys] || 1;
}
arr.forEach(countFunc);
// {1: 2, 2: 2, 3: 2, 4: 1}
Upvotes: 9
Reputation: 1176
You may want to use indexOf()
function to find and count each value
in array
function g(array,value){
var n = -1;
var i = -1;
do {
n++;
i = array.indexOf(value, i+1);
} while (i >= 0 );
return n;
}
Upvotes: 0
Reputation: 13896
You could use reduce to get there:
var a = [1,2,3,1,2,3,4];
var map = a.reduce(function(obj, b) {
obj[b] = ++obj[b] || 1;
return obj;
}, {});
Upvotes: 32