morne
morne

Reputation: 4189

filter array of objects not returning filtered values

I have this array of objects.

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

I want to filter the array to only contain objects that has a short_name2 of the given/passed 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);
},

But I end up with an empty array everytime. What am I missing?

I tried the following aswell.

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

Upvotes: 2

Views: 64

Answers (3)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can try like this with Array.prototype.filter() to filter out only short_name2 is equal to your passed code e.g e. Also add a check for short_name2 variable like this way short_name2!="" to non-empty checking.

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

Upvotes: 1

James
James

Reputation: 22247

As mentioned by @yonexbat you should just be able to remove the erroneous parameter from your .filter callback:

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

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

    console.log(newList);
}

Upvotes: 0

bumbaneitr11
bumbaneitr11

Reputation: 101

Have you tried renaming the code variable inside the filter function as index?

If it's due to scope, it'll take the closest scope variable name into game, comparing item with code, which is the index of the item in the array.

Upvotes: 1

Related Questions