我想知道如何在map函数中使用常量,其基本含义是:我已经正确地从下拉菜单中保存了有关常量的选项(我用console.log进行了检查),例如,我有一个选择了名称,然后我想在map函数中使用它,但是不幸的是,当我使用常量时,所有元素都未定义;当我用直接写的“名称”替换常量时,我用它们的名称正确地获取了所有元素。
Filterhosts=() =>{
var newState = this.state.array.slice(); // in the state array is empty
const selectedOption = this.state.selectedOption;
const writtenOption = this.state.writtenOption;
console.log(selectedOption) //ok
const namearray= this.state.filteredhosts.map(host=> {
return (
host.software.map((sub, subindex) => {
if(selectedOption=="name" || selectedOption=="vendor") {
newState.push(sub.selectedOption) //when I write sub.selectedOption , I receive empty array with all elements as undefined otherwise I become the names of all elements
}
else {
if(sub.vulnerable==true){
newState.push(sub.vulnerability.cve)}
}
})
)
})
const filteredarray = newState.filter( function(item){
return item === writtenOption // here I become properly the searched name//vendor
}
// how to show the whole info for the searched name/vendor(cpe, cve, cvss etc.)
)
console.log(newState); //ok
console.log(filteredarray); //ok
}
答案 0 :(得分:0)
我知道了。
sub.name
与
相同sub["name"]
也与
相同sub[selectedOption]
如果selectedOption为"name"
。因此,只需使用newState.push(sub[selectedOption])
,我认为它应该对您有用。