代码之家  ›  专栏  ›  技术社区  ›  morne

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

  •  2
  • morne  · 技术社区  · 6 年前

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

    但每次我都会得到一个空数组。我错过了什么?

    我也试过以下方法。

    _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);
    },
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   A l w a y s S u n n y    6 年前

    你可以用这个试试 Array.prototype.filter() 只过滤掉 short_name2 code 例如 . 同时添加检查 短名称2 像这样变化无常 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
  •  1
  •   bumbaneitr11    6 年前

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

        3
  •  0
  •   James    6 年前

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

    _filterList(code){
        let originalList = this.props.choiceOfOpioidsList;
    
        let newList = originalList.filter(function (item) {
            return item.short_name2.toLowerCase() === code;
        });
    
        console.log(newList);
    }