Newbie here so please excuse the ignorance. I wanted to know why my resulting variable is an empty string, when I loop through the array using forEach and concat method.
const concatSpeakersText = arr => {
const speakerOneText = ''
const speakerTwoText = ''
arr.forEach(utterance => {
utterance.speaker === '1' ? speakerOneText.concat(' ', utterance.text) : speakerTwoText.concat(' ', utterance.text)
})
console.log(speakerOneText)
console.log(speakerTwoText)
}
concat()returns a new string, you need to assign it back to the variable.concat. This has nothing to do withforEach. You could’ve reduced your minimal reproducible example tolet str = ""; str.concat("string"); console.log(str);.