简体   繁体   English

拼接数组返回空结果

[英]splice array returns empty reults

I am trying to use splice top remove the last 2 items from an array. 我试图使用拼接顶部从数组中删除最后2个项目。 It always returns nothing. 它始终不返回任何内容。 Trying to figure out what i am missing 试图找出我所缺少的

 function planets_remove() { var planetsArray = []; planetsArray.push(document.getElementById("planets").value); var martian = planetsArray.splice(4,2)[0]; document.getElementById("planets_removed").innerHTML = martain; } 
 Enter 7 Planets<br> <textarea rows="5" cols="50" id="planets" name"planets"></textarea> <br><br> Click to remove the last 2 items from the array<br> <button type="button" class="processButton" onclick="planets_remove()">Remove</button><br> <p id="planets_removed"><p> <button type="button" class="processButton" onclick="planets_remove()">Remove</button><br> 

Try this Javascript code: 试试这个Javascript代码:

function planets_remove() {
  var planetsArray = document.getElementById("planets").value.split('\n');
  var martian = planetsArray.splice(0, planetsArray.length - 2);
  document.getElementById("planets_removed").innerHTML = martian;
}

The problem was, you were not pushing 7 elements, you were just pushing one multi-line string to an array. 问题是,您没有推送7个元素,而只是将一个多行字符串推送到一个数组。

You have to split the the string, so that it converts to individual elements. 您必须拆分字符串,以便将其转换为单个元素。 And then you can splice it. 然后可以拼接它。

Your array only ever contains one element (at most). 您的数组最多只能包含一个元素。

Solution: add a button to add new planet to the array after every input 解决方案:添加按钮以在每次输入后向阵列添加新的星球

Enter 7 Planets<br>
<textarea rows="5" cols="50" id="planets" name"planets"></textarea><br><br>
<button type="button" class="processButton" onclick="planets_add()"> Add </button><br>

Click to remove the last 2 items from the array<br>
<button type="button" class="processButton" onclick="planets_remove()"> Remove </button><br>
<p id="planets_removed"><p>

var planetsArray = [];
function planets_add() {
  planetsArray.push(document.getElementById("planets").value);
}

function planets_remove() {
  var martian = planetsArray.splice(4,2)[0];
  document.getElementById("planets_removed").innerHTML = martain;
}

You have to split textarea value by some splitter, here is example with space(' '). 您必须通过一些分割器分割textarea值,这是带有space('')的示例。

 function planets_remove() { var planetsTxt = document.getElementById("planets"); var planetsArray = planetsTxt.value.split(' '); var martian = planetsArray.splice(planetsArray.length - 2, 2); document.getElementById("planets_removed").innerHTML = martian.join(' '); planetsTxt.value = planetsArray.join(' '); } 
 Enter 7 Planets<br> <textarea rows="5" cols="50" id="planets" name"planets"></textarea> <br><br> Click to remove the last 2 items from the array<br> <button type="button" class="processButton" onclick="planets_remove()">Remove</button><br> <p id="planets_removed"><p> <button type="button" class="processButton" onclick="planets_remove()">Remove</button><br> 

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

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