Javascript 对属性名称中包含数字的对象进行排序和筛选
我试图筛选出Javascript 对属性名称中包含数字的对象进行排序和筛选,javascript,arrays,reactjs,react-native,sorting,Javascript,Arrays,Reactjs,React Native,Sorting,我试图筛选出模式设置为空的项目,但是,添加此过滤器后,所有项目都会消失,即使它们在空处没有模式属性 const filteredSortedBotConfigs=Object.keys(botConfigs) .排序((a,b)=>{ 返回parseInt(a,10)-parseInt(b,10); }) .filter(此.FilterConfigBybot) .filter(此.FilterConfigByStatus) .filter((项)=>item.mode==null); 假设bo
模式设置为空的项目,但是,添加此过滤器后,所有项目都会消失,即使它们在空处没有模式属性
const filteredSortedBotConfigs=Object.keys(botConfigs)
.排序((a,b)=>{
返回parseInt(a,10)-parseInt(b,10);
})
.filter(此.FilterConfigBybot)
.filter(此.FilterConfigByStatus)
.filter((项)=>item.mode==null);
假设botConfig
数据是以数字作为属性的对象:
const botConfigs={
2:{模式:null,botId:“10”,状态:“活动”,},
1:{mode:“A”,botId:“20”,status:“ACTIVE”},
3:{模式:“C”,botId:“15”,状态:“停止”},
};
您需要按(数字)属性排序,然后根据值的属性进行筛选。因此,过滤器的功能可能是这样的:
filterConfigsByBot = (key) => {
return botConfigs[key].botId !== "0"; // assuming botConfigs is available in scope
};
filterConfigsByStatus = (key) => {
return botConfigs[key].status !== "STOPPED";
};
此外,请记住在末尾将键映射到botConfigs
的值(如果需要):
PS:您可以将三个过滤器组合成一个过滤器回调
编辑:
简化版本,使用:
您能否发布函数filterConfigsByBot
和filterConfigsByStatus
的代码?这可能有助于我们调试您的状态我正在尝试筛选出“mode”props设置为null的项,但是您的代码却做了相反的事情,只允许null的模式。此外,还有两个其他的过滤函数您还没有显示,所以我们不确定这些函数在做什么。看起来这要么是因为简单的输入错误而需要关闭,要么是需要添加更多信息。>“假设botConfig数据是以数字为属性的对象”对象键是字符串。@ArunKumarMohan如果您看到这样令人困惑的注释,请随意编辑我的答案。我将感谢你的努力它可能会给你带来好处。
const filteredAndSortedBotConfigs = Object.keys(botConfigs)
.sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
.filter(this.filterConfigsByBot)
.filter(this.filterConfigsByStatus)
.filter((key) => botConfigs[key].mode !== null) // to filter out items that has the `mode` set to `null`
.map((key) => botConfigs[key]);
const filteredAndSortedBotConfigs = Object.keys(botConfigs)
.sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
.reduce((acc, curr) => {
if (
botConfigs[curr].botId !== "0" &&
botConfigs[curr].status !== "STOPPED" &&
botConfigs[curr].mode !== null
) {
acc.push(botConfigs[curr]); // pushing values, not keys
}
return acc;
}, []);