Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不返回筛选值的对象的筛选数组_Javascript_Reactjs - Fatal编程技术网

Javascript 不返回筛选值的对象的筛选数组

Javascript 不返回筛选值的对象的筛选数组,javascript,reactjs,Javascript,Reactjs,我有这个对象数组 (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {tbi_tblid: 512100013, long_name: "", short_name: "", short_name2: "", trickysort: "", …} 1: {tbi_tblid: 512100013, long_name: "Diamorphine", short_name: "07", short_name2: "", trickysort:

我有这个对象数组

(9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {tbi_tblid: 512100013, long_name: "", short_name: "", short_name2: "", trickysort: "", …}
1: {tbi_tblid: 512100013, long_name: "Diamorphine", short_name: "07", short_name2: "", trickysort: "Diamorphine", …}
2: {tbi_tblid: 512100013, long_name: "Fentanyl", short_name: "06", short_name2: "P", trickysort: "Fentanyl", …}
3: {tbi_tblid: 512100013, long_name: "Fentanyl  2 mcg/ml", short_name: "02", short_name2: "E", trickysort: "Fentanyl  2 mcg/ml", …}
4: {tbi_tblid: 512100013, long_name: "Fentanyl 4 mcg/ml", short_name: "03", short_name2: "E", trickysort: "Fentanyl 4 mcg/ml", …}
5: {tbi_tblid: 512100013, long_name: "Morphine", short_name: "04", short_name2: "P", trickysort: "Morphine", …}
6: {tbi_tblid: 512100013, long_name: "No Opioid", short_name: "01", short_name2: "", trickysort: "No Opioid", …}
7: {tbi_tblid: 512100013, long_name: "Other", short_name: "08", short_name2: "", trickysort: "Other", …}
8: {tbi_tblid: 512100013, long_name: "Oxycodone", short_name: "05", short_name2: "", trickysort: "Oxycodone", …}
length: 9
__proto__: Array(0)
我想过滤数组,使其仅包含具有给定/传递的
code
short\u name2
的对象

_correctOpioidOptions(type){
    if(type === 'epidural'){
        return {choiceOfOpioidsList_epi:this._filterList('e')}
    }else if(type === 'pca'){
        return {choiceOfOpioidsList_pca:this._filterList('p')}
    }
},
_filterList(code){
    let originalList = this.props.choiceOfOpioidsList;

    let newList = originalList.filter(function (item,code) {
        return item.short_name2.toLowerCase() === code;
    });

    console.log(newList);
},
但每次我都会得到一个空数组。我错过了什么

我也尝试了以下方法

_filterList(code){
    let originalList = this.props.choiceOfOpioidsList;

    let newList = originalList.filter(function (item,code) {
        if(return item.short_name2.toLowerCase() === code){
           return item;
        }
        return false;
    });

    console.log(newList);
},

您是否尝试过将筛选函数中的代码变量重命名为索引


如果是由于作用域的原因,它会将最近的作用域变量名带入游戏,将物品与代码进行比较,代码是数组中物品的索引。

你可以这样尝试,只过滤掉
短名称2
等于你传递的
代码
e.ge。还可以像这样为
short\u name2
变量添加一个检查
short\u name2=“”
非空检查

const arr_tblid:512100013,“long_name”:“short_name”:“short_name 2”:“trickysort”:“},{”tbi_tblid:512100013,“long_name”:“Diamorphine”,“short_name”:“07”,“short_name 2”:“trickysort”:“Diamorphine”},“trickysort”:“Diamorphine”},{“tbi_tblid”:“long_name”:“fentany:512100013,“长名称”:“芬太尼2 mcg/ml”,“短名称”:“02”,“短名称2”:“E”,“trickysort”:“芬太尼2 mcg/ml”),{“tbi tblid”:512100013,“长名称”:“芬太尼4 mcg/ml”,“短名称”:“03”,“短名称2”:“E”,“trickysort”:“芬太尼4 mcg/ml”,{“tbi tbi tblid”:51210013,“长名称”:“吗啡”,“短名称”:“04”,“短名称”,“吗啡”;“trickysort”:{“tbi_tblid”:512100013,“long_name”:“No阿片类”、“short_name”:“01”、“short_name 2”:“trikysort”:“No阿片类”}、{“tbi_tblid”:512100013,“long_name”:“Other”、“short_name”:“08”、“short_name 2”:“trikysort”:“Other”}、{“tbi_tbi_tblid”:51210013,“long_name”:“Oxycodone”、“short_name”:“05”、“short_name 2”:“trikysort”:“Oxycodone”}]
让代码='e';
result=arr_obj.filter((el,i)=>el.short_name2!=“”&&el.short_name2.toLowerCase()==code)

console.log(result);
正如@yonexbat所提到的,您应该能够从您的.filter回调中删除错误的参数:

_filterList(code){
    let originalList = this.props.choiceOfOpioidsList;

    let newList = originalList.filter(function (item) {
        return item.short_name2.toLowerCase() === code;
    });

    console.log(newList);
}

不清楚您实际上是如何调用
\u filterList
,但是传递给
filter
方法的回调的第二个参数引用了正在测试的数组元素的索引-请参见此处:。这意味着将忽略传递给
\u filterList
参数(回调的
return
语句中的
code
没有引用此参数)。这可能是它不起作用的原因-如果起作用,修复方法可能只是将函数签名更改为
函数(项)
。基于此:有三个参数通过筛选器内部代码传入数组。筛选器回调,它们是元素、索引和数组。
code
,作为第二个参数,接收正在处理的数组项的索引值,这不是您所期望的。is
\u filterlist(code){..stuff},
react缩写为
\u filterlist:function(code){..stuff},
或者这就是问题的一部分?@morne很高兴它能帮助你。祝你好运:)先生。编码愉快。