如何过滤地图内的三元结果?
How can I filter the result of a ternary that's inside a map?
我是三元世界的100000%新手,所以我真的不明白这是怎么回事。我有以下内容:
categories.forEach((category) => {
const errorCategory = {
[category]: response.map(
(r) => r.ds_category === category && r.ds_name
),
};
errors = { ...errors, ...errorCategory };
});
我想过滤 r.ds_name 以删除所有“错误”结果,我该怎么做?我试过了:
立即进行过滤 (r.ds_name.filter(blablabla)),但出现“这不是函数”错误。
从三元函数切换到旧学校函数,也没有得到任何结果。
过滤 errorCategory (errorCategory = errorCategory.filter((item) => item === "false"),但我也得到了“这不是一个函数”的错误。
谢谢:(
您可以像这样在过滤方法中使用三元运算符
const newArray = array.filter( element => {
element.property === "text" ? true : false
})
它检查 element.property 是否等于“文本”。如果相等,则 return 为真。如果不是 returns false。但你不需要那样做。你可以简单地这样做
const newArray = array.filter( element => {
element.property === "text"
})
这会return相同的结果
要过滤数组,我建议使用“.filter”而不是“.map”。这将删除与要求集不匹配的元素。假设 response 是一个数组对象,或其他实现 'filter' 的对象,您可以执行以下操作(注意额外的括号):
categories.forEach( ( category ) => {
const errorCategory = {
[ category ]: response.filter(
(r) => (r.ds_category === category && r.ds_name)
),
};
//Do stuff
}
我是三元世界的100000%新手,所以我真的不明白这是怎么回事。我有以下内容:
categories.forEach((category) => {
const errorCategory = {
[category]: response.map(
(r) => r.ds_category === category && r.ds_name
),
};
errors = { ...errors, ...errorCategory };
});
我想过滤 r.ds_name 以删除所有“错误”结果,我该怎么做?我试过了:
立即进行过滤 (r.ds_name.filter(blablabla)),但出现“这不是函数”错误。
从三元函数切换到旧学校函数,也没有得到任何结果。
过滤 errorCategory (errorCategory = errorCategory.filter((item) => item === "false"),但我也得到了“这不是一个函数”的错误。
谢谢:(
您可以像这样在过滤方法中使用三元运算符
const newArray = array.filter( element => {
element.property === "text" ? true : false
})
它检查 element.property 是否等于“文本”。如果相等,则 return 为真。如果不是 returns false。但你不需要那样做。你可以简单地这样做
const newArray = array.filter( element => {
element.property === "text"
})
这会return相同的结果
要过滤数组,我建议使用“.filter”而不是“.map”。这将删除与要求集不匹配的元素。假设 response 是一个数组对象,或其他实现 'filter' 的对象,您可以执行以下操作(注意额外的括号):
categories.forEach( ( category ) => {
const errorCategory = {
[ category ]: response.filter(
(r) => (r.ds_category === category && r.ds_name)
),
};
//Do stuff
}