首页 > 解决方案 > 打字稿 - 在过滤器中添加动态字段

问题描述

如何在过滤器中动态添加 n 个字段?我想根据我的要求传递 1 个字段或 2 个字段或 n 个字段..

myArray.filter(x => x.field1 === field1Value && x.field2 === field2Value &&  so on depending on my requirements..);

标签: angulartypescript

解决方案


一种方法可以是

let attrValues = [{attr:'name', value:'Marcus'}, { attr:'age', value=24}]
    
    myArray.filter(x=>{
    return attrValues.every(item=>x[item.attr] == item.value)
    })

推荐阅读