'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!
Solution 1:[1]
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?
Solution 2:[2]
it should be
const data = [
["Val #","Val #","Val #","Val #","Val #"],
["SO-000379-001A-047-1","SO-000379-001A-047-2","-","-","-"]
]
const result = data.map(d => d.filter(v => !['', '-'].includes(v)))
console.log(result)
you want to filter out the inner arrays not the main one
so you have to map each external array and then filter the single rows
Solution 3:[3]
Is this what you need? Filter items based on x and y axis.
const cads = [
["Val #", "Val #", "Val #3", "Val #", "Val #"],
["SO-000379-001A-047-1", "-", "SO-000379-001A-047-2", "-", "-"]
]
function f(data) {
if (!(data && data.length)) {
return []
}
const v = []
const m = data.length;
const n = data[0].length;
const check = (x) => x !== '-' && x !== ''
for (let i = 0; i < n; i++) {
let f = true
for (let j = 0; j < m; j++) {
if (!check(data[j][i])) {
f = false
break;
}
}
if (f) {
for (let k = 0; k < m; k++) {
v[k] = v[k] || []
v[k].push(data[k][i]);
}
}
}
return v
}
console.log(f(cads))
Solution 4:[4]
Description
I believe what you are looking for is a way to filter both rows of the array based on the items in the second row. Here is an example of how to do that. I changed your data slightly to show that it is filtering row 1.
Script
function test() {
try {
let data = [ ["Val 1","Val 2","Val 3","Val 4","Val 5"],
["SO-000379-001A-047-1","","SO-000379-001A-047-2","-","-"] ];
let result = [[],[]];
data[1].forEach( (item,i) => {
if( (item != "-") && (item != "") ) {
result[0].push(data[0][i]);
result[1].push(data[1][i]);
}
});
console.log(result);
}
catch(err) {
console.log(err);
}
}
Console.log
8:23:57 AM Notice Execution started
8:23:58 AM Info [ [ 'Val 1', 'Val 3' ],
[ 'SO-000379-001A-047-1', 'SO-000379-001A-047-2' ] ]
8:23:57 AM Notice Execution completed
Solution 5:[5]
It's not very clear from the question what you are trying to achieve. The following script is the simplest way since the latest ECMA to filter such a nested array. The criteria I used here is to filter out strings that only contain "-"
but you can modify it to add/remove other criteria.
let array = [
["Val #","Val #","Val #","Val #","Val #"],
["SO-000379-001A-047-1","SO-000379-001A-047-2","-","-","-"]
]
let filtered = [];
for (e of array) {
let kept = [];
for (item of e) {
if (item != "-") kept.push(item);
}
filtered.push(kept);
}
console.log(filtered);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tin Po Chan |
Solution 2 | R4ncid |
Solution 3 | Leon |
Solution 4 | TheWizEd |
Solution 5 |