Can anyone explain in detail what exactly is going on in x[y] pairs? I tried console logging but no useful info. I am particularly interested in the operation within the if block.
let arr = ['a','b','b','c','c','c']
let obj = {}
arr.forEach((element) => {
if (obj[element]){
obj[element]++
}else{
obj[element] = 1
}
})
console.log(obj) //{a: 1, b: 2, c: 3}
so the IF blocks:
if(obj[element]) just means if element exists as a key in obj.
Here, if element does not exist within obj, create a new key called element, assign it the value of 1 (currently one instance of it existing)
If element exists as a key within obj, add 1 to the key element in obj (to increase the count)