Javascript:如何从索引为 0 的数组中拼接一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5175556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: How do I splice a value from an array with an index of 0?
提问by dottedquad
I am attempting to remove a value from an array using splice. starting at 0 and ending at 0 splice, but it is not removing the value at index 0. I added a function getItemRow to check the species index which returns 0. I dumped the values of the array into an alert and it still outputs species which should of been deleted. invalidElement.splice(indexValue, indexValue); works as expected for indexes that are NOT 0. Why is this happening and how do I delete the value that has 0 index?
我正在尝试使用 splice 从数组中删除一个值。从 0 开始到 0 拼接结束,但它没有删除索引 0 处的值。我添加了一个函数 getItemRow 来检查返回 0 的物种索引。我将数组的值转储到警报中,它仍然输出物种应该被删除。invalidElement.splice(indexValue, indexValue); 对于非 0 的索引,按预期工作。为什么会发生这种情况以及如何删除具有 0 索引的值?
javascript code:
javascript代码:
var invalidElement = new Array("species", "alias", "gender", "breeding", "birth_date");
//This function will be removed once fixed!!
function getItemRow()
{
var myPosition=-1
for (i=0;i<invalidElement.length;i++)
{
if(invalidElement[i]=="species") {
myPosition = i;
break;
}
}
alert(myPosition)
}
function validateElement(formId, element, selector, errorContainer)
{
getItemRow()//for testing purposes
//var indexValue = $.inArray(element, invalidElement);
var indexValue = invalidElement.indexOf(element);
alert(element);
$.ajax({
type: 'POST',
cache: false,
url: "validate_livestock/validate_form/field/" + element,
data: element+"="+$(selector).val(),
context: document.body,
dataType: 'html',
success: function(data){
if (data == "false")
{
$(errorContainer).removeClass('element_valid').addClass('element_error');
invalidElement = element;
alert(invalidElement.join('\n'))//for testing purposes
//alert(indexValue);
}
else
{
$(errorContainer).removeClass('element_error').addClass('element_valid');
invalidElement.splice(indexValue, indexValue);
alert(invalidElement.length);//for testing purposes
alert(invalidElement.join('\n'))//for testing purposes
}
}
});
}
$("#species").change(function(){
validateElement('#add_livestock', 'species', '#species', '.species_error_1')
});
回答by alex
回答by David Tang
There's also a convenience function for removing the first element in an array:
还有一个方便的函数用于删除数组中的第一个元素:
array.shift();
See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/shift.
请参阅:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/shift。
回答by STW
Splice can work in two modes; to remove or insert items.
拼接可以工作在两种模式;删除或插入项目。
When removing items you'll specify two parameters: splice(index, length)
where index is the starting index, and length is a positive number of elements to remove (fyi: passing a "0", as in your example, does nothing--it's saying "remove zero items starting at index"). In your case you'll want:
删除项目时,您将指定两个参数:splice(index, length)
其中 index 是起始索引,length 是要删除的元素的正数(仅供参考:在您的示例中传递“0”,什么都不做——它说“删除零从索引开始的项目")。在你的情况下,你会想要:
invalidElement.splice(indexValue, 1); // Remove 1 element starting at indexValue
When inserting items you'll specify (at least) three parameters: splice(index, length, newElement, *additionalNewElements*)
. In this overload you normally pass 0
as a 2nd parameter, meaning to insert the new elements between existing elements.
插入项目时,您将指定(至少)三个参数:splice(index, length, newElement, *additionalNewElements*)
. 在此重载中,您通常0
作为第二个参数传递,这意味着在现有元素之间插入新元素。
var invalidElements = ["Invalid2", "Invalid3"];
invalidElements = invalidElements.splice(0, 0, "Invalid1");
回答by typo.pl
Mozilla Dev Center - Array.splicestates that the second parameter is 'howMany' elements to remove.
Mozilla 开发中心 - Array.splice指出第二个参数是要删除的“howMany”元素。
I'm not sure how your code worked when it passed in the indexValue as the number of elements to remove, unless you were removing from the end of the array.
我不确定您的代码在传入 indexValue 作为要删除的元素数时如何工作,除非您从数组的末尾删除。