检查值等于数组中的特定值 - javascript

时间:2018-05-03 09:30:10

标签: javascript arrays loops object conditional-statements

我有以下对象

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

我需要检查数组值是否等于特定值。

我需要检查list PRICElistCOUNTRY还是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.
        }
    })

如何实现这一目标?

3 个答案:

答案 0 :(得分:0)

您的关注点是遍历所有可能的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());
}

答案 1 :(得分:0)

您可以编写过滤器函数并将其用于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
    )
);

答案 2 :(得分:0)

希望这会有所帮助



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