简体   繁体   English

如何过滤对象数组的 arrays 数组?

[英]How to filter array of arrays of array of objects?

I am trying to filter an array of arrays of objects, but it does not work.我正在尝试过滤 arrays 个对象的数组,但它不起作用。

   constructNewGrid(filterText){
      if(searchText == ""){
        this.constructedGrid = this.fullGrid;
        return;
      }
      this.constructedGrid = [];
      this.constructedGrid = this.fullGrid.filter(array => array.filter(obj => {
        if(obj.name == filterText)
          return obj;
      }));
      console.log(this.constructedGrid);
   }

I want to return an array of arrays with the correct object if found one but when I console log it constructedGrid is the same array but why?我想返回一个 arrays 的数组,如果找到一个正确的 object,但是当我控制台记录它时, constructedGrid是同一个数组,但为什么呢? The code should go to the first array should look up whether there is an object with the name equals to the filter text it should return the array with the corrosponding object and then the next array should be checked and so on.代码应该 go 到第一个数组应该查找是否有名称等于过滤器文本的 object 它应该返回具有相应 object 的数组,然后应该检查下一个数组等等。

This, would be the format:这将是格式:

[ 
  [[{name: string}], empty, empty], // array of lenth 3 
  [empty, empty, [{name: string}], empty], // array of length 4 
   ... 
]

If one object is found it should push it in an array separately such that if two objects where found in the same array, it should put them both in separately push them in two separat arrays and those should be in one single array: Result should be如果找到一个 object,它应该将它分别放入一个数组中,这样如果在同一个数组中找到两个对象,它应该将它们分别放入两个单独的 arrays 中,并且它们应该在一个数组中:结果应该是

[
 [[obj1]],
 [[obj2]],
 ...
]

It seems in possible for me.这对我来说似乎是可能的。 I got sometime an error that I am out of memory haha...有时我得到一个错误,我没有 memory 哈哈......

You'll need map in addition to filter , because filter just decides whether to keep what's there, it doesn't change what's there.除了filter之外,您还需要map ,因为filter只是决定是否保留那里的内容,它不会改变那里的内容。 map does. map可以。 Something like this:是这样的:

  this.constructedGrid = this.fullGrid
      // Map the inner array to one that only has matching entries
      .map(array => array.filter(obj => obj && obj.name === filterText))
      // Remove blank inner arrays from the overall array
      .filter(array => array.length);

Live Example:现场示例:

 const fullGrid = [ [[{name: "target"}], , ], [, null, [{name: "target"}], undefined], ]; const filterText = "target"; const constructedGrid = fullGrid // Map the inner array to one that only has matching entries.map(array => array.filter(obj => obj && obj[0] && obj[0].name === filterText)) // Remove blank inner arrays from the overall array.filter(array => array.length); console.log(constructedGrid);
 .as-console-wrapper { max-height: 100%;important; }

Note that you only need that second filter if you want to remove arrays that are completely empty from the outer array.请注意,如果要从外部数组中删除完全为空的 arrays,则只需要第二个filter If you want to leave them, just remove that call.如果您想离开他们,只需删除该电话即可。 Edit: From your reply to my question on the question, it sounds like you want to remove that second .filter .编辑:从你对我关于这个问题的问题的回复来看,听起来你想删除第二个.filter

Note the guard in the first filter , the obj && obj[0] && part.注意第一个filter中的守卫,即obj && obj[0] &&部分。 It's there because you said sometimes array entries are "empty."它在那里是因为你说有时数组条目是“空的”。 I don't know if you literally meant empty — a sparse array — or entries that are undefined .我不知道您的字面意思是空的(稀疏数组)还是undefined的条目。 If you literally meant empty (a sparse array), you don't need the guard, but it's probably best to have it.如果你的字面意思是的(一个稀疏数组),你不需要守卫,但最好有它。

As of ES2020, you could use the optional chaining operator instead:从 ES2020 开始,您可以改用可选的链接运算符:

.filter(obj => obj?.[0]?.name === filterText)

obj?.[0]?.name evaluates to undefined if obj is null or undefined , or obj[0] is null or undefined ;如果objnullundefined ,或者obj[0]nullundefinedobj?.[0]?.name计算结果为undefined it evaluates to obj[0].name otherwise.否则它的计算结果为obj[0].name Since undefined === filterText will be false, entries with a null or undefined obj will be left out.由于undefined === filterText将为 false,带有nullundefined obj的条目将被排除在外。 Again, though, new feature, you'll need to check support on your target browsers if you're not transpiling.不过,同样是新功能,如果您不进行转译,则需要检查目标浏览器的支持情况。

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

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