After spending years dabbling in VBA, I am now delving into Typescript.
I currently have an array of binary strings
Each string represents a binary number
My goal is to filter the array and extract all strings that contain '1' at position X
I have successfully written a traditional function to accomplish this task
function CountOnes( ipArray: string[], ipIndex: number): number
{
var myCount:number=0
for (var ipString of ipArray)
{
if (ipString[ipIndex]==="1") { myCount+=1}
}
return myCount
}
However, I am struggling to achieve the same result using the .filter method. (where myData is the array of strings and myIndex is the position)
myCount = myData.filter((x:string, myIndex:number) => x[myIndex]==="1").length)
I'm unsure about what I am missing. Can someone suggest a solution using the .reduce method?
It's important to note that I have been able to use the filter method in both VBA and Nim to achieve similar goals (VBA required me to create a FilterIt class), so I am puzzled as to why I am facing difficulties in Typescript/Javascript.