简体   繁体   English

根据打字稿中的“输入”条件过滤数组元素

[英]Filtering array elements based on 'in' condition in typescript

I have an arrayList which contains employees information like employeename, grade, designation. 我有一个arrayList,其中包含员工信息,例如员工姓名,职称,职位。 I have a multiselect component in my view which returns an array of grades like [1,2,3] once we choose grade1 , grade2 , grade3 from the multiselect dropdown. 我有我的观点一个多选组件返回等级的像一个数组[1,2,3]一旦我们选择grade1grade2grade3从多选下拉列表。 Is there a way to filter my employeelist based on these grades array? 有没有一种方法可以根据这些成绩数组来过滤我的员工名单? Similar like this: 类似这样:

this.employeeList.filter(x=> x.grade in (grade1,grade2,grade3));

Or is there otherway to acheive this? 还是有其他方法可以做到这一点? Basically I need to filter my employeelist based on multiselect values. 基本上,我需要根据多重选择值过滤我的员工列表。 Please suggest as I am new to typescript. 请提出建议,因为我是打字稿新手。 Your help is very much appreciated. 非常感激你的帮助。

When you deal with regular variables, you can just wrap them into additional array [var1, var2, ...] and use includes() for check: 处理常规变量时,可以将它们包装到其他数组[var1, var2, ...]并使用includes()进行检查:

 const grade1 = 1, grade2 = 2, grade3 = 3; const employeeList = [ { grade: 1 }, { grade: 3 }, { grade: 4 }, { grade: 8 }, { grade: 9 } ]; const result = employeeList.filter(x => [grade1,grade2,grade3].includes(x.grade)); console.log(result) // should print the array with objects {"grade": 1} and {"grade": 3} 

我只会使用类似:

x => [grade1,grade2,grade3].indexOf(x) != -1

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

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