[英]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. 我需要检查
list
是否具有PRICE
或list
具有COUNTRY
或list
具有PRICE,LIST,COUNTRY
组合。
Currently I'm using includes
which checks if value is present. 目前,我正在使用
includes
来检查是否存在值。 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. 您的关注是遍历所有可能的
if
条件。
So you shouldn't do return
within a condition checking. 因此,您不应该在条件检查中
return
。
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 您可以组成过滤器函数,并将其用于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) } } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.