【问题标题】:Slicing nodes within array在数组中切片节点
【发布时间】:2019-07-30 01:28:35
【问题描述】:

当我第一次从第二个选择菜单中选择两个选项时,数组将填充这两个选项。我想要的是第二个选择的选项替换第一个,所以即使我从第二个选择菜单中选择两个选项开始,数组的长度将保持为一个,在这些选择之间动态变化。我希望你明白。谢谢你的帮助。我知道我可以让它成为一个功能,这个问题不会存在,但我无法使用它。

var select1 = document.getElementById('select1');
var select2 = document.getElementById('select2');
var array = []
        
function myFunct1() { 
  var one = select1.options[select1.selectedIndex].value;
  array.splice(0, 1, one);
  console.log(array);
        }
        
function myFunct2() {
  var two = select2.options[select2.selectedIndex].value;
  array.splice(1, 1, two);
  console.log(array);
        }
<select id = 'select1' onchange = 'myFunct1()'>
        <option disabled selected value> -- select an option -- </option>
        <option value = 'Dog'>ONE</option>
        <option value = 'Cat'>TWO</option>
        <option value = 'Bear'>THREE</option>
        </select>
    
        
    <select id = 'select2' onchange = 'myFunct2()'>
        <option disabled selected value> -- select an option -- </option>
        <option value = 'Dog'>ONE</option>
        <option value = 'Cat'>TWO</option>
        <option value = 'Bear'>THREE</option>
        </select>

【问题讨论】:

  • 如果按下array = ["One","Two"]TWO 会发生什么。数组应更改为["Two","One"] 或保持["One","Two"]
  • 对,但我的问题是按两次时出现的。
  • 您希望保留数组。 ["One","Two"] 还是改成这个["Two","One"]

标签: javascript html arrays list slice


【解决方案1】:

首先使用Array.prototype.unshift() 来增加价值。您可以检查数组中是否存在元素 使用includes()
您可以创建相同的函数并向其传递不同的参数,而不是创建两个函数。

var array = [];
        
        function myFunct(val){ 
            if(!array.includes(val)) array.unshift(val);
            console.log(array);
        }
<button onclick = 'myFunct("One")'>ONE</button>
        <button onclick = 'myFunct("Two")'>TWO</button>

如果您想用第一个值替换新值,请使用此代码

function myFunct(val) {
     array.unshift(val);
     array = [... new Set(array)];
     console.log(array); 
}

更新:

var select1 = document.getElementById('select1');
var select2 = document.getElementById('select2');
var array = [];
let sel1 = false;

function myFunct1() { 
  var one = select1.options[select1.selectedIndex].value;
  if(array.length === 1 && !sel1) array.unshift(one);
  else array.splice(0,1,one);
  console.log(array);
  sel1 = true;
}

function myFunct2() {
  var two = select2.options[select2.selectedIndex].value;
  array.splice(sel1, 1, two);
  console.log(array);
}

【讨论】:

  • 但是,如果我使用的不是总是按“一”和“二”的按钮,而是使用我希望它记住并替换每个选择菜单中的选项的选择菜单,但通过这种方法它除非您重新单击相同的选项,否则不会替换。
  • @Harper Creek 你没有回答我的问题如果按下array = ["One","Two"]TWO 会发生什么。数组应该更改为["Two","One"] 还是保持["One","Two"]
  • 我的意思是在示例中它变成了 ["Two","One"]?你是在问这对我想做的事情是否重要?我想它没有。如果两个人留在第一个位置,我可能会更方便。
  • @Harper Creek 好的,现在请编辑您的问题需要更多帮助
  • @Harper Creek 我已经更新了代码。如果还有问题请告诉我
【解决方案2】:

试试这个:

var array = [];
        
function myFunct() {
  if(array.indexOf('One') === -1 ) { array.unshift('One') ; }    
  console.log(array);
}
        
function myFunct2() {
  if(array.indexOf('Two') === -1 ) { array.push('Two') ; }
  console.log(array);
}
<button onclick = 'myFunct()'>ONE</button>
<button onclick = 'myFunct2()'>TWO</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 2012-11-10
    • 1970-01-01
    • 2017-05-31
    • 2019-01-23
    • 2018-02-05
    • 2021-09-04
    • 2021-10-29
    相关资源
    最近更新 更多