简体   繁体   English

为什么这个二维数组过滤不起作用?

[英]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? WTHeck我失踪了吗?

Thank you!谢谢!

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你想过滤掉内部的 arrays 而不是主要的

so you have to map each external array and then filter the single rows所以你必须 map 每个外部数组然后过滤单行

Is this what you need?这是你需要的吗? Filter items based on x and y axis.基于 x 和 y 轴过滤项目。

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

 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?不够聪明,无法使用那些 Array.map/filter function,但我想这就是您想要的?

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.我稍微更改了您的数据以表明它正在过滤第 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

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.以下脚本是自最新 ECMA 以来过滤此类嵌套数组的最简单方法。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM