Javascript-指定数组中的所有其他索引元素(调用的元素除外)

Javascript-指定数组中的所有其他索引元素(调用的元素除外),javascript,arrays,Javascript,Arrays,我试图实现的是,以某种方式将数组中的所有索引值组合在一起,而不是调用。为了澄清,我编写了以下代码 第一个document.write按预期工作,并打印与按下按钮相关的索引字符串。对于第二个document.write,我想打印除已单击/选择的索引元素之外的所有其他索引元素。如何将所有其他索引元素组合在一起?不用说,对于所有Javascript大师来说,myarray[!clicked])在代码中的尝试都不起作用 <html> <body> <script>

我试图实现的是,以某种方式将数组中的所有索引值组合在一起,而不是调用。为了澄清,我编写了以下代码

第一个
document.write
按预期工作,并打印与按下按钮相关的索引字符串。对于第二个
document.write
,我想打印除已单击/选择的索引元素之外的所有其他索引元素。如何将所有其他索引元素组合在一起?不用说,对于所有Javascript大师来说,
myarray[!clicked])
在代码中的尝试都不起作用

<html>

<body>
<script>
var myarray = ["button1", "button2", "button3"];
function pushed(clicked) {

for (var i=0; i<myarray.length; i++) {

document.write("the button that has been pushed is" + myarray[clicked]);
document.write("the buttons that have not been pushed are" + myarray[!clicked]);
}
}
</script>

<button onclick="pushed(0)"> Button 1 </button>
<button onclick="pushed(1)"> Button 2 </button>
<button onclick="pushed(2)"> Button 3 </button>

</body>
</html>

var myarray=[“button1”、“button2”、“button3”];
推送函数(单击){

对于(var i=0;i您应该打印单击的按钮,然后打印关于未单击按钮的句子。然后您可以使用单击按钮的索引和条件将其从正在打印的其他按钮中排除

document.write("the button that has been pushed is" + myarray[clicked]);
document.write("the buttons that have not been pushed are: ");

for (var i = 0; i < myarray.length; i++) {
    if (i !== clicked) {
        document.write(myarray[i]);       
    }
}
document.write(“按下的按钮是”+myarray[点击]);
document.write(“未按下的按钮为:”);
对于(var i=0;i
您应该先打印单击的按钮,然后打印关于未单击按钮的句子。然后您可以使用单击按钮的索引和条件将其从正在打印的其他按钮中排除

document.write("the button that has been pushed is" + myarray[clicked]);
document.write("the buttons that have not been pushed are: ");

for (var i = 0; i < myarray.length; i++) {
    if (i !== clicked) {
        document.write(myarray[i]);       
    }
}
document.write(“按下的按钮是”+myarray[点击]);
document.write(“未按下的按钮为:”);
对于(var i=0;i
您不需要遍历数组,只需执行以下操作:

var clicked = myarray[clicked];
var notClicked = myarray.filter(function(item) {
    return item !== clicked;
});
document.write('clicked is ' + clicked);
document.write('not clicked are ' + notClicked);

您不需要遍历数组,只需执行以下操作:

var clicked = myarray[clicked];
var notClicked = myarray.filter(function(item) {
    return item !== clicked;
});
document.write('clicked is ' + clicked);
document.write('not clicked are ' + notClicked);
试试这个:

function pushed(clicked) {

document.write("the button that has been pushed is" + myarray[clicked]);
document.write("the buttons that have not been pushed are" + myarray.filter((elt, i) => i !== clicked).join(", "));
}
试试这个:

function pushed(clicked) {

document.write("the button that has been pushed is" + myarray[clicked]);
document.write("the buttons that have not been pushed are" + myarray.filter((elt, i) => i !== clicked).join(", "));
}

过滤器在数组中进行迭代。是的,我指的是原始代码段中的循环。过滤器在数组中进行迭代。是的,我指的是原始代码段中的循环。这似乎不起作用,当我出于某种原因使用此按钮时,不会发生任何事情。您将其放置在按下(单击)的
按钮中
function?我正在试图了解这是如何工作的,目前我不明白第二行的末尾为空时,第二行如何打印剩余的两个按钮信息?它没有。它“打印”未按下的按钮是:。循环打印实际的按钮。这似乎不起作用,当我出于某种原因使用此按钮单击按钮时,不会发生任何事情。您将其放置在按下(单击)的
按钮中
功能?我正在试图了解这是如何工作的,目前我不明白第二行在行尾为空时如何打印剩余的两个按钮信息?它没有。它“打印”未按下的按钮是:。循环打印实际按钮。