Idiomatically Find the Number of Occurrences a Given Value Has in an Array

Idiomatically find the number of occurrences a given value has in an array

reduce is more appropriate here than filter as it doesn't build a temporary array just for counting.

var dataset = [2,2,4,2,6,4,7,8];var search = 2;
var count = dataset.reduce(function(n, val) { return n + (val === search);}, 0);
console.log(count);

How to find the number of repetitions of an item in an array in JavaScript?

Try this solution

 function getOccurrence(myArray, value) {
var count = 0;
myArray .forEach((val) => (val === value && count++));
return count;
}

Count string occurrences in an array

You could for example use a Map to count the number of occurrences. Use the first item from split as the key and start with 1.

For every same key, increment the value by 1.

Assuming all the values have a an underscore present:

const folders = ["a_x_y_1", "b_x_y_1", "b_x_y_3", "b_x_y_2", "c_x_y_1", "c_x_y_2", "d_x_y_1"];
const m = new Map();

const result = folders.forEach(folder => {
const part = folder.slice(0, folder.lastIndexOf('_'));
!m.has(part) ? m.set(part, 1) : m.set(part, m.get(part) + 1);
})

for (const [key, value] of m.entries()) {
console.log(`${key}~${value}`);
}

Find all occurrences of word in array

Try this:

var dataset = ["word", "a word", "another word"];
var search = "word";
count = 0;

jQuery(dataset).each(function(i, v){ if(v.indexOf(search) != -1) {count ++} });

Here, count will be 3.

Count number of items in an array of objects with a specific property value (JavaScript)

D3 would cope with either result format - it does slightly depend on what you want to do with it. Regardless, here is some example code for generating either format:

var result = {};
array.forEach(function(i) {
if (!result.hasOwnProperty(i.created)) {
result[i.created] = 0;
}
if (i.status === 1) {
result[i.created] += 1;
}
});

var newArrayA = Object.keys(result)
.map(function(k) {
var o={};
o[k] = result[k];
return o;
})

var newArrayB = Object.keys(result)
.map(function(k) { return [k,result[k]] })

How can I find the top n occurrences of an object value in an array using Typescript/JS?

You could get an object with the count first by building an array of objects, sort it descending by occurences and slice the array for getting top three elements only.

var data = [{ intent: "hello", other_value: "blah" }, { intent: "hello", other_value: "blahblah" }, { intent: "hi", other_value: "anothervalue" }, { intent: "hello", other_value: "what?" }, { intent: "hello", other_value: "eh?" }, { intent: "hi", other_value: "okthen" }, { intent: "yo", other_value: "alright" }, { intent: "hi", other_value: "yes" }, { intent: "yo", other_value: "yawhat?" }, { intent: "hey", other_value: "meh" }],    count = data        .reduce((r, { intent }) => {            r[intent] = r[intent] || { intent, occurences: 0 };            r[intent].occurences++;            return r;        }, {}),    top3 = Object        .values(count)        .sort((a, b) => b.occurences - a.occurences)        .slice(0, 3);    console.log(top3);console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Related Topics



Leave a reply



Submit