function selectAndList(propertyName, inputArr) {
const propertyArr = [];
inputArr.map(function(item) {
propertyArr.push(item[propertyName]);
});
return propertyArr;
}
function countOccurences(propertyArr, value){
const values =propertyArr.filter(function(prop){
return prop==value
})
return values.length
}
function countOccurences(propertyArr){
const map=new Map()
propertyArr.map(function(val){
if(map.has(val)){
map.set(val, map.get(val)+1)
}else{
map.set(val, 0)
}
})
return map
}
Apologies for this previous misunderstanding. I have rewritten my code in addition as to not use arrow functions as you mentioned you are not familiar with this. I thought it best to mention this as this may be useful when looking up info online. This is how such works for information, this was introduced in ES6:
function myFunc(val){
}
const myFunc = (val)=>{
}
You would call both with myFunc(val), this does not change.
also you should use const and let instead of var, var is defined globally, even when within a function.
I have rewritten the function to reduce time complexity. There is no need to create multiple arrays. I have also defined a function to count occurences for a given property value