I'm attempting to add a condition to my 2D array that I am getting back from an API call. The data being returned is a top 3 rankings for state. So for example, there are 3 values that read "California" with the second index being the rank. I am attempting to apply a condition to filter out any state that does not return appear 3 times, for example: If (state name) does not have 1, 2, 3 dont return. My issue is that I do not know how to apply this logic successfully.
I've attempted looping through an array and putting a condition if the state name does not happen 3 times, remove from the array.
My expected result would be this :
const data = [ [ "California", 1 , 23432], ["Califonria", 2, 22132], ["California", 3, 49343], ["New York", 1, 3231], ["New York", 2 , 3023], ["New York", 3, 2932]
here is a snippet that i've tried:
const data = [ [ "California", 1 , 23432], ["Califonria", 2, 22132], ["California", 3, 49343], ["New York", 1, 3231], ["New York", 2 , 3023], ["New York", 3, 2932], ["Georgia", 1, 3423]]
for (let i = 0; data.length > i; i++) {
if (data[0].length < 3) {
data.splice(i,1)
}
}
console.log(data)