gpt4 book ai didi

javascript - 过滤不返回过滤值的对象数组

转载 作者:行者123 更新时间:2023-11-29 20:49:17 25 4
gpt4 key购买 nike

我有这个对象数组。

(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)

我想过滤数组以仅包含具有给定/传递的 codeshort_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);
},

最佳答案

你可以用 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);

关于javascript - 过滤不返回过滤值的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com