![](/img/trans.png)
[英]How to check if a value is not equal to another value in the array using javascript
[英]Check value is equal to specific value in an array - javascript
我有以下对象
members
{
[
age:30,
list: [
"PRICE",
"LIST",
"COUNTRY"
]
]
},
{
[
age:31,
list: [
"PRICE"
]
]
},
{
[
age:31,
list: [
"LIST"
]
]
}
我需要检查数组值是否等于特定值。
我需要检查list
是否具有PRICE
或list
具有COUNTRY
或list
具有PRICE,LIST,COUNTRY
组合。
目前,我正在使用includes
来检查是否存在值。 但是我需要检查确切的值
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.
}
})
如何做到这一点?
您的关注是遍历所有可能的if
条件。
因此,您不应该在条件检查中return
。
您可以保存结果,并在匹配条件时覆盖它。 希望能帮上忙。
您的原始成员对象也存在一些问题。 在以下演示中也已修复。
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()); }
您可以组成过滤器函数,并将其用于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)) );
或者,也许您正在寻找以下内容:
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 ) );
希望这会有所帮助
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) } } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.