我想过滤一个数组,只获取HasAccess设置为false的值。唯一的问题是HasAccess并不总是存在。有时,设置为空。我该如何处理?
0:{
ClientCodeRegional: (2) ["29000-A", "122214"]
CreatedUpdatedBy: null
Email: "testuserthree@for.deletion"
Id: "11ad1dfb-3cd6-4db6-87a9-8063bb12504d"
Settings: {
CsmPassword: ""
CsmUser: ""
HasAccess: false
IsAdmin: true
IsInternal: false
IsLocked: true
MarketAreaCode: ""
Role: "Administrator"
Updated: null
UpdatedBy: null
UserId: "11ad1dfb-3cd6-4db6-87a9-8063bb12504d"
}
}
我尝试了很多不同的方法,但这就是我目前所获得的。
const users = result.data.filter((number) => {
//number.Settings != null
if (typeof number.Settings == "null") {
return true;
} else {
return number;
}
})
答案 0 :(得分:3)
如果要获取所有已定义Settings.HasAccess
且为false
的条目,请使用逻辑和(&&
)来检查{首先存在{1}}个对象,然后使用settings
将HasAccess
属性与false
进行比较。
仅当===
存在且true
属性设置为false时,Settings
才会返回。
HasAccess
如果要获取所有未定义const result = {
data: [
{ Settings: { HasAccess: true }, a: 1 },
{ Settings: { HasAccess: false }, a: 2 },
{ Settings: { }, a: 3 },
{ a: 4 }
]
};
const users = result.data.filter(item => {
return item.Settings && item.Settings.HasAccess === false;
})
console.log(users);
或虚假的结果,请使用not运算符(HasAccess
来代替(||
) )。
如果满足以下条件,则返回true:
!
不存在Settings
属性未定义HasAccess
存在,但其值是虚假的(null,undefined,false)
HasAccess
答案 1 :(得分:1)
我想过滤一个数组,仅获取HasAccess为的值 设置为false,有时设置为空
只需检查Settings && (HasAccess === false)
let arr = [{ClientCodeRegional:["29000-A","122214"],CreatedUpdatedBy:null,Email:"testuserthree@for.deletion",Id:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d",Settings:{CsmPassword:"",CsmUser:"",HasAccess:false,IsAdmin:true,IsInternal:false,IsLocked:true,MarketAreaCode:"",Role:"Administrator",Updated:null,UpdatedBy:null,UserId:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d",}},{ClientCodeRegional:["29000-A","122214"],CreatedUpdatedBy:null,Email:"testuserthree@for.deletion",Id:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d",Settings:{CsmPassword:"",CsmUser:"",HasAccess:null,IsAdmin:true,IsInternal:false,IsLocked:true,MarketAreaCode:"",Role:"Administrator",Updated:null,UpdatedBy:null,UserId:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d"}},{ClientCodeRegional:["29000-A","122214"],CreatedUpdatedBy:null,Email:"testuserthree@for.deletion",Id:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d",Settings:{CsmPassword:"",CsmUser:"",//HasAccess:null,
IsAdmin:true,IsInternal:false,IsLocked:true,MarketAreaCode:"",Role:"Administrator",Updated:null,UpdatedBy:null,UserId:"11ad1dfb-3cd6-4db6-87a9-8063bb12504d"}}]
let op = arr.filter(({Settings})=> Settings && (Settings.HasAccess === false))
console.log(op)
答案 2 :(得分:0)
您可以尝试以下操作:
/**
* Calculates "Cartesian Product" sets.
* @example
* cartesianProduct([[1,2], [4,8], [16,32]])
* Returns:
* [
* [1, 4, 16],
* [1, 4, 32],
* [1, 8, 16],
* [1, 8, 32],
* [2, 4, 16],
* [2, 4, 32],
* [2, 8, 16],
* [2, 8, 32]
* ]
* @see https://stackoverflow.com/a/36234242/1955709
* @see https://en.wikipedia.org/wiki/Cartesian_product
* @param arr {T[][]}
* @returns {T[][]}
*/
function cartesianProduct<T> (arr: T[][]): T[][] {
return arr.reduce((a, b) => {
return a.map(x => {
return b.map(y => {
return x.concat(y)
})
}).reduce((c, d) => c.concat(d), [])
}, [[]] as T[][])
}