简体   繁体   English

在Javascript中检查键值对

[英]Checking Key Value Pairs in Javascript

I have a Business Scenario as below. 我有一个商业场景如下。

I am adding rows of possible (key, value) pairs.(Country, Climate) like one below. 我正在添加可能(键,值)对的行。(国家,气候)如下所示。

In Image 2 the whole scenario Valid and Invalid (Key, Value) are given. 在图2中,给出了整个场景Valid和Invalid(Key,Value)。


Possible Keys - All Country, India, Australia, America, England 可能的钥匙 - 所有国家,印度,澳大利亚,美国,英国

Possible Values - All Climate, Hot, Dry, Rainy, Cold, Humid 可能的价值观 - 所有气候,炎热,干燥,多雨,寒冷,潮湿


For Example 例如

if (All Country, All Climate) is chosen as first pair (England, hot) should not be allowed to be chosen as second pair if(All Country,All Climate)被选为第一对(英格兰,热门)不应该被选为第二对

If (All Country, hot) is chosen as first pair (India, hot) should not be allowed to bechosen as second pair 如果选择(所有国家/地区,热门)作为第一对(印度,热)不应该被选为第二对

If (America, All Climate) is chosen as first pair (America, Hot) should not be allowed to be chosen as second pair 如果选择(America,All Climate)作为第一对(America,Hot),则不应该选择第二对

where as 在哪里

If (India, hot) is chosen as first pair (India, humid) shall be allowed chosen as second pair 如果选择(印度,热)作为第一对(印度,潮湿),则允许选择第二对

If (America, All Climate) is chosen as first pair (England, All Climate) shall be be allowed chosen as second pair 如果选择(America,All Climate)作为第一对(英格兰,All Climate),则应选择第二对

If (America, All Climate) is chosen as first pair (India, humid) shall be allowed to be chosen as second pair 如果选择(America,All Climate)作为第一对(印度,潮湿),则允许选择第二对

Image 1 图片1

此搜索


Image 2 图2

镜像2

Question I have explained My Colleague all the valid and Invalid possible combinations through the Image2. 问题我通过Image2向My Colleague解释了所有有效和无效的可能组合。

In javascript the simplest solution would be to add if else statements and getting it done.By doing so the gap between the one I explained to solve Business Scenario in paper and code widens. 在javascript中,最简单的解决方案是添加if else语句并完成它。通过这样做,我解释的用于解决纸张和代码中的业务场景的问题之间的差距扩大了。

What would be the best implementation of transferring the Matrix in the Paper to Code so the business in the paper and code have a close relation. 将纸张中的矩阵转移到代码中的最佳实施方式是什么,因此纸张和代码中的业务关系密切。

This question may sound vague but we all at least once faced issue like this where things written to solve a problem in paper and the way it solved by code makes no sense. 这个问题可能听起来含糊不清,但我们至少曾经遇到过这样的问题,即用纸张解决问题的方式以及用代码解决问题的方式都没有意义。

Use binary arithmetic. 使用二进制算术。

Country values 国家价值观

India       0001
Australia   0010
America     0100
England     1000
All Country 1111

Climate Value 气候价值

Hot         00001
Dry         00010
Rainy       00100
Cold        01000
Humid       10000
All Climate 11111

When you choose second item, use AND operation to decide if that value is already choosen, (ALL or same value). 当您选择第二项时,使用AND操作来确定是否已选择该值(全部或相同的值)。

example

var firstCountry = 0xF
var secondCountry = 0x1
if ( (firstCountry & secondCountry) > 0)
{
    console.log("country is already choosen");
}

Of course this example is only for one attribute. 当然,此示例仅适用于一个属性。 You should update it for both attributes. 您应该为两个属性更新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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