@nknahom/lotide
Version:
Lotide is mini clone of the Lodash (https://lodash.com) library for learning JavaScript. What is Lodash? Lodash is a library that makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
25 lines (22 loc) • 686 B
JavaScript
// allItems: an array of strings that we need to look through
// itemsToCount: an object specifying what to count
const countOnly = function (allItems, itemsToCount) {
const results = {};
for (const item of allItems) {
// inside the loop,
// increment the counter for each item:
// set a property with that string key to:
// the value that was already there (or zero if nothing there) with 1 added to it.
// inside the loop:
if (itemsToCount[item]) {
if (results[item]) {
results[item] += 1;
} else {
results[item] = 1;
}
}
}
console.log("results", results);
return results;
};
module.exports = countOnly;