I have an array that contains comma separated numbers in a string, a sample array looks like this
const arr = ["2", "1,2", "2,3", "1,3"]
I want to create another array which contains the sum of the frequency of occurrence of these values.
Here is the algo I am trying to implement
- loop through each element, via a reducer that has an accumulator array
- split each element and convert them into an array of numbers
- if the accumulator index is empty, initialize it to zero, otherwise add 1, to pre-existing value
Here is the code, I am trying
arr.reduce((acc, item) => {
item.split(",").map((value, index) => {
val = parseInt(value)
acc[val] === null ? acc[val] = 0 : acc[val] += 1
})
})
I get an error Uncaught TypeError: Cannot read property 2 of undefined