不返回过滤值的对象的过滤器数组

时间:2018-10-09 15:46:51

标签: 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: "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)

我想过滤数组以仅包含具有给定/传递的short_name2的{​​{1}}的对象

code

但是我每次都会得到一个空数组。我想念什么?

我也尝试了以下方法。

_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);
},

3 个答案:

答案 0 :(得分:1)

您是否尝试过将过滤器函数内的代码变量重命名为索引?

如果是由于作用域引起的,它将把最接近的作用域变量名称带入游戏中,将项目与代码进行比较,该代码是数组中项目的索引。

答案 1 :(得分:1)

您可以尝试使用Array.prototype.filter()这样来过滤,仅过滤出short_name2等于您传递的code,例如 e 。还要像这样short_name2添加对short_name2!=""变量的检查,以非空检查。

const arr_obj = [{"tbi_tblid":512100013,"long_name":"","short_name":"","short_name2":"","trickysort":""},{"tbi_tblid":512100013,"long_name":"Diamorphine","short_name":"07","short_name2":"","trickysort":"Diamorphine"},{"tbi_tblid":512100013,"long_name":"Fentanyl","short_name":"06","short_name2":"P","trickysort":"Fentanyl"},{"tbi_tblid":512100013,"long_name":"Fentanyl  2 mcg/ml","short_name":"02","short_name2":"E","trickysort":"Fentanyl  2 mcg/ml"},{"tbi_tblid":512100013,"long_name":"Fentanyl 4 mcg/ml","short_name":"03","short_name2":"E","trickysort":"Fentanyl 4 mcg/ml"},{"tbi_tblid":512100013,"long_name":"Morphine","short_name":"04","short_name2":"P","trickysort":"Morphine"},{"tbi_tblid":512100013,"long_name":"No Opioid","short_name":"01","short_name2":"","trickysort":"No Opioid"},{"tbi_tblid":512100013,"long_name":"Other","short_name":"08","short_name2":"","trickysort":"Other"},{"tbi_tblid":512100013,"long_name":"Oxycodone","short_name":"05","short_name2":"","trickysort":"Oxycodone"}]
let code = 'e';
result = arr_obj.filter((el,i)=>el.short_name2!="" && el.short_name2.toLowerCase()===code)
console.log(result);

答案 2 :(得分:0)

如@yonexbat所述,您应该只能够从.filter回调中删除错误的参数:

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

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

    console.log(newList);
}