简体   繁体   中英

Check value is equal to specific value in an array - javascript

I have the below object

members
    {
        [
            age:30,
            list: [
                "PRICE",
                "LIST",
                "COUNTRY"
            ]
        ]
    },
    {
        [
            age:31,
            list: [
                "PRICE"
            ]
        ]
    },
    {
        [
            age:31,
            list: [
                "LIST"
            ]
        ]
    }

I need to check if array values are equal to specific value.

I need to check if list has PRICE or list has COUNTRY or list has PRICE,LIST,COUNTRY combination.

Currently I'm using includes which checks if value is present. But i need to check exact value

Array.isArray(members.list.map(message, index){
        if(message.includes("LIST"))
        {

        }
        if(message.includes("PRICE"))
        {

        }
         if(message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY"))
        {
            //message.includes("PRICE") and ("LIST") is already executed, so this will execute again. But i need to execute the complete condition combination.
        }
    })

How to acheive this?

Your concern is to loop through all possible if condition.
So you shouldn't do return within a condition checking.
You can save the result and override it whenever it matches a condition. Hope can help.
Also there is some problem of your origin members Object. Also fixed in the following demo.

 let members = [ { age: 30, list: ["PRICE", "LIST", "COUNTRY"] }, { age: 31, list: ["PRICE"] }, { age: 31, list: ["LIST"] } ]; Array.prototype.inArray = function () { var message = this; if (message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY")) { return "contains PRICE, LIST and COUNTRY"; } if (message.includes("PRICE") && message.includes("LIST") ) { return "contains PRICE and LIST "; } if (message.includes("PRICE") && message.includes("COUNTRY")) { return "contains PRICE and COUNTRY"; } if (message.includes("LIST") && message.includes("COUNTRY")) { return "contains LIST and COUNTRY"; } if (message.includes("LIST")) { return "contains LIST"; } if (message.includes("PRICE")) { return "contains PRICE"; } if (message.includes("LIST")) { return "contains LIST"; } } for (let member of members) { console.log(member.list.inArray()); } 

You can compose a filter function and use that for Array.prototype.filter

 const members = [ { age: 30, list: ["PRICE","LIST","COUNTRY"] }, { age: 31, list: ["PRICE"] }, { age: 31, list: ["LIST"] }, { age: 88, list: ["not this one"] }, { age: 88, list: ["not this one","COUNTRY","NOTPRICEORLIST"] } ] const getList = o => (o&&o.list) || [];//get list member from o or returns empty array const contains = needle => haystack => haystack.includes(needle);//check if haystack contains needle const containsAll = needles => haystack => needles.reduce( (result,needle)=>result && haystack.includes(needle), true ); const countryOrPriceOrPriceListCountry = haystack => contains("PRICE")(haystack) || contains("LIST")(haystack) //this is pointless, would already be true because it contains price or country // || containsAll(["PRICE","LIST","COUNTRY"])(haystack); const filter = getter => comparer => item =>//get something from item (list) and send to compare function comparer(getter(item)); console.log( members.filter(filter(getList)(countryOrPriceOrPriceListCountry)) ); 

Or maybe you were looking for the following:

 const members = [ { age: 30, list: ["PRICE","LIST","COUNTRY"] }, { age: 31, list: ["PRICE"] }, { age: 32, list: ["LIST"] }, { age: 88, list: ["not this one"] }, { age: 89, list: ["not this one","COUNTRY","NOTPRICEORLIST"] } ] const getList = o => (o&&o.list) || [];//get list member from o or returns empty array const contains = needle => haystack => haystack.includes(needle);//check if haystack contains needle const containsAll = needles => haystack => needles.reduce( (result,needle)=>result && haystack.includes(needle), true ); const countryOrPrice = haystack => contains("PRICE")(haystack) || contains("LIST")(haystack) const countryListAndPrice = containsAll(["PRICE","LIST","COUNTRY"]); members.map( item=>[item,countryOrPrice(getList(item))]//see if item has country or price ).map( ([item,hasCountryOrPrice])=>[ item, hasCountryOrPrice, // will check for country list and price only if has country or price hasCountryOrPrice && countryListAndPrice(getList(item)) ] ).forEach( ([item,hasCountryOrPrice,countryListAndPrice])=> console.log( "item age:", item.age, "has country or price:", hasCountryOrPrice, "has country list and price:", countryListAndPrice ) ); 

Hope this will help

 var members = [ { age: 30, list: ["PRICE", "LIST", "COUNTRY"] }, { age: 31, list: ["PRICE"] }, { age: 31, list: ["LIST"] } ]; members.forEach((val,key)=>{ if(Array.isArray(val.list)){ if(val.list.indexOf('PRICE') > -1 || val.list.indexOf('COUNTRY') > -1 || (val.list.indexOf('PRICE') > -1 && val.list.indexOf('COUNTRY') > -1 && val.list.indexOf('LIST') > -1)){ console.log(val) } } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM