繁体   English   中英

我想让一个javascript数组成为条件

[英]I would like to make a javascript array conditional

在JavaScript中,我想使myArray的内容成为条件(我想使用if语句。我尝试这样做但无济于事。

我的一些代码可供参考:

<?php 
    session_start();
    $theirname = $_GET['theirname'];
    $yourname = $_GET['yourname'];
    $slct2 = $_GET['slct2'];
    $slct1 = $_GET['slct1'];
?>

<script type="text/javascript">
    var theirName = "<?php echo $theirname; ?>";
    var yourName = "<?php echo $yourname; ?>";
    var cardType = "<?php echo $slct1; ?>";
    var personType = "<?php echo $slct2; ?>";

    var myArray=[
        "i need", 
        "to make the contents of ",  
        "this array",  
        "conditional",  
        "to the variable",  
        "cardType",  
        "and the variable",  
        "personType", 
    ];

    //shuffle array:
    myArray.sort(function(){return Math.round(Math.random());});

    //print to screen
    function printGreet(){
        document.getElementById('demo').innerHTML= [myArray.pop()];
    }
</script>

<div>
    <button onclick="printGreet()" class="large-btn" id="generate-Btn" rows="20">Generate</button>
</div>
<div id="yourMes">
    <p>Your message:</p>
</div>
<textarea name="text1" class="large-fld" id="demo"></textarea>

您可以这样做:

var myArray = [];

if(theirName  === "John"){ myArray.push("You")}
if(yourName   !== "Jane"){ myArray.push("get")}
if(cardType   >=   3    ){ myArray.push("the")}
if(personType ===  5    ){ myArray.push("idea...")}

如果满足特定条件,则只会将特定项添加到数组中。

例如,如果只有yourNamepersonType条件通过,则myArray将为:

["get", "idea..."];

暂无
暂无

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

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