简体   繁体   English

Javascript检查数组中的每个元素

[英]Javascript check against each element in an array

I have this code:我有这个代码:

var newStatus = (status, subStatus) => {
  if(status === 'CREATED' && 
         (subStatus === 'subStatus1' || 'subStatus2' || 'subStatus3')){
    return 'CONFIRMED';
  } else if(status === 'CANCELLED' && 
         (subStatus === 'subStatus4' || 'subStatus5' || 'subStatus6')){
    return 'REMOVED';
  } else if(status === 'REVIEW' && 
        (subStatus === 'subStatus7' || 'subStatus8' || 'subStatus9')){
    return 'CHECKED';
  }
}

<div>newStatus('CREATED', 'subStatus2')</div>

In this case, the div must render with the value 'CONFIRMED'.在这种情况下,div 必须使用值 'CONFIRMED' 呈现。 I believe this logic has a bug and I need 'subStatus ' to be an array and check somehow against each element, but how can I achieve this?我相信这个逻辑有一个错误,我需要 'subStatus' 作为一个数组并以某种方式检查每个元素,但我怎样才能做到这一点?

Can someone help me with a solution on how to implement this logic correctly?有人可以帮助我解决如何正确实现此逻辑的解决方案吗?

You can create a map of the sub-statuses and then, for the unique sub-status, check that it also has the expected status and, if so, return the new status:您可以创建子状态的映射,然后对于唯一的子状态,检查它是否也具有预期状态,如果是,则返回新状态:

 subStatuses = { "subStatus1": { "expectedStatus": "CREATED", "newStatus": "CONFIRMED" }, "subStatus2": { "expectedStatus": "CREATED", "newStatus": "CONFIRMED" }, "subStatus3": { "expectedStatus": "CREATED", "newStatus": "CONFIRMED" }, "subStatus4": { "expectedStatus": "CANCELLED", "newStatus": "REMOVED" }, "subStatus5": { "expectedStatus": "CANCELLED", "newStatus": "REMOVED" }, "subStatus6": { "expectedStatus": "CANCELLED", "newStatus": "REMOVED" }, "subStatus7": { "expectedStatus": "REVIEW", "newStatus": "CHECKED" }, "subStatus8": { "expectedStatus": "REVIEW", "newStatus": "CHECKED" }, "subStatus9": { "expectedStatus": "REVIEW", "newStatus": "CHECKED" }, } function newStatus( status, subStatus ) { if ( subStatus in subStatuses && status === subStatuses[subStatus].expectedStatus ) { return subStatuses[subStatus].newStatus; } return undefined; } element = document.getElementById( "to_check" ); element.innerText = newStatus( element.getAttribute( "status" ), element.getAttribute( "substatus" ) );
 <div id="to_check" status="CREATED" substatus="subStatus2" />

You could use an object to map each subarray to the correct status, this way you don't need to add multiple if statements for all the possible outcomes.您可以使用一个对象将每个子数组映射到正确的状态,这样您就不需要为所有可能的结果添加多个 if 语句。 You can just update the object.您可以只更新对象。

 const statusMap = { created: { sub: ['subStatus1', 'subStatus2', 'subStatus3'], value: 'CONFIRMED', }, cancelled: { sub: ['subStatus4', 'subStatus5', 'subStatus6'], value: 'REMOVED', }, review: { sub: ['subStatus7', 'subStatus8', 'subStatus9'], value: 'CHECKED', }, }; const newStatus = (status, subStatus, statusMap) => { // Turn the status to lowercase const lower = status.toLowerCase(); // Return if it doesn't exist if(!statusMap[lower]) return; // Get sub array and return value const {sub, value} = statusMap[lower]; // If the item exist in the sub array return the value if(sub.includes(subStatus)) return value; } const status1 = newStatus('CREATED', 'subStatus2', statusMap); const status2 = newStatus('CANCELLED', 'subStatus6', statusMap); const status3 = newStatus('REVIEW', 'subStatus7', statusMap); console.log(status1); console.log(status2); console.log(status3);

There are big issues with your code.您的代码存在大问题。 This comparison:这个比较:

subStatus === 'subStatus1' || 'subStatus2' || 'subStatus3'

will return true if subStatus has the value of 'subStatus1' .如果subStatus的值为'subStatus1'则返回true If not, it will return true because 'subStatus2' will be interpreted as true.如果不是,它将返回true因为'subStatus2'将被解释为 true。
If you want to check if subStatus has one of the values of subStatus1 or subStatus2 or subStatus1 , then you should include those values in an array like this:如果要检查subStatus是否具有subStatus1subStatus2subStatus1的值之一,则应将这些值包含在如下数组中:

const SUB_STATUSES = ['subStatus1', 'subStatus2', 'subStatus3'];

then, you will have to use JavaScript's array method includes() like this:然后,您将不得不像这样使用 JavaScript 的数组方法includes()

SUB_STATUSES.includes(subStatus)

that will return true if the value of subStatus is included in SUB_STATUSES .如果subStatus的值包含在SUB_STATUSES ,则返回true You can check the documentation here Array.prototype.includes()您可以在此处查看文档Array.prototype.includes()

Your code should look something like this after the modifications:修改后,您的代码应如下所示:

const SUB_STATUSES1 = ['subStatus1', 'subStatus2', 'subStatus3'];
const SUB_STATUSES2 = ['subStatus4', 'subStatus5', 'subStatus6'];
const SUB_STATUSES3 = ['subStatus7', 'subStatus8', 'subStatus9'];
const newStatus = (status, subStatus) => {
    if (
        status === 'CREATED' && SUB_STATUSES1.includes(subStatus)
    ) {
        return 'CONFIRMED';
    }
    if (
        status === 'CANCELLED' && SUB_STATUSES2.includes(subStatus)
    ) {
        return 'REMOVED';
    }
    if (
        status === 'REVIEW' && SUB_STATUSES3.includes(subStatus)
    ) {
        return 'CHECKED';
    }
};

Apart from that, there might be another issues because there might be cases that slips through these conditions.除此之外,可能还有另一个问题,因为可能有一些情况会绕过这些条件。

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

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