Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report

0

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?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!