Skip to content
Advertisement

Why is this 2D array filtering not working?

I have this 2D array and I’m trying to filter it, but it’s coming out unfiltered:

Data:

[
 ["Val #","Val #","Val #","Val #","Val #"],
 ["SO-000379-001A-047-1","SO-000379-001A-047-2","-","-","-"]
]

Filtering line:

cads = cads.filter(e => e[1] != "-" || e[1] != '');

Expected result:

[
 ["Val #","Val #"],
 ["SO-000379-001A-047-1","SO-000379-001A-047-2"]
]

WTHeck am I missing?

Thank you!

Advertisement

Answer

let data = [
 ["Val #","Val #","Val #","Val #","Val #"],
 ["SO-000379-001A-047-1","SO-000379-001A-047-2","-","-","-"]
]

for (let i = 0; i <= data[1].length; i++) {
    if(data[1][i] === "-"){
        data[0].splice(i,1);
        data[1].splice(i,1);
        i--;
    }
}

console.log(data);

Not smart enough to use those Array.map/filter function, but I guess this is what you want?

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement