【问题标题】:Check value is equal to specific value in an array - javascript检查值等于数组中的特定值 - javascript
【发布时间】:2018-10-13 13:29:34
【问题描述】:

我有下面的对象

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

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

我需要检查list 是否有PRICElist 是否有COUNTRYlist 是否有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.
        }
    })

如何做到这一点?

【问题讨论】:

  • Array.isArray(members.list.map(...)) 没有多大意义,因为.map() 总是会返回一个数组 O.o
  • 查看这个帖子,对link有帮助
  • @Andreas 那么解决方案应该是什么?
  • 先把比较变成最具体的,然后用else:if(message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY")) {} else if(message.includes("LIST")) {} else if(message.includes("PRICE")) { }
  • @siraxtas 但该帖子未显示有关检查组合

标签: javascript arrays loops object conditional-statements


【解决方案1】:

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

【讨论】:

  • 我希望 PRICEPRICE, LIST, COUNTRY 组合执行.. 在您的代码中,两者都将执行
  • 对不起,我误解了你的问题。最愚蠢的解决方案是编辑后的答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    希望这会有所帮助

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2023-01-31
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多