javascript 如何将新项目“推”到数组的中间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48145421/
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
How to "push' a new item to the middle of an array?
提问by Hey24sheep
I just completed section1, and noticed that there isn't a method covered on how to push an item to the a specific location of the array. For example, if I wanted the array to show
我刚刚完成了第 1 部分,并注意到没有涵盖如何将项目推送到数组的特定位置的方法。例如,如果我希望数组显示
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"]
How would I be able to push in "Brooks Brothers" into the position [2] of the array suits, and shift the rest 1 down? Is there a built-in function in javascript similar to push that would enable me to do this?
我如何才能将“布鲁克斯兄弟”推入阵列套装的位置 [2],并将其余的 1 向下移动?javascript 中是否有类似于 push 的内置函数可以让我做到这一点?
I suppose I could always, with some diffculty:
我想我总是可以,但有些困难:
function add (item, position){
var length = suits.length;
for(i = length -1; i >= position; i--){
suits[length] = suits[i];
length--;
};
suits[position] = item;
};
add("Brooks Brothers",2) //to add it to the middle
回答by felixmosh
You can use Array.splicein order to insert item to Array at specific place.
您可以使用Array.splice以将项目插入到特定位置的 Array 中。
const suits = ["hearts", "clubs", "Brooks Brothers", "diamonds", "spades"];
suits.splice(2, 0, 'newItem');
console.log(suits);
回答by CodeIt
There are no inbuilt function for this in JavaScript but you can simply do this using splice
JavaScript 中没有为此内置函数,但您可以使用splice简单地做到这一点
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"];
suits.splice(2, 0, "Brooks Brothers");
This would insert item X to index 2 of array suits, ["hearts", "clubs", "Brooks Brothers", "Brooks Brothers", "diamonds", "spades"]
这会将项目 X 插入数组套装的索引 2, ["hearts", "clubs", "Brooks Brothers", "Brooks Brothers", "diamonds", "spades"]
Syntax
句法
<array-name>.splice(<position-to-insert-items>,0,<item-1>,<item-2>,..,<item-n>)
Always pass second second argument as 0, because we don't want to delete any item from the array while splicing.
始终将第二个第二个参数传递为 0,因为我们不想在拼接时从数组中删除任何项目。
回答by Hey24sheep
You can use the built-in SpliceFunction
您可以使用内置的拼接功能
The splice()method changes the contents of an array by removing existing elements and/or adding new elements.
的拼接()方法通过去除现有元件和/或添加新元素改变的数组的内容。
1- To insert single value
1- 插入单个值
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"];
//1st param is insert index = 2 means insert at index 2
//2nd param is delete item count = 0 means delete 0 elements
//3rd param is new item that you want to insert
suits.splice(2, 0 , "Test");
console.log(suits);
2- To insert array into your suits array
2- 将数组插入您的西装数组
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"];
var newSuitsToInsert = ["test1", "test2","hello"];
//1st param is insert index = 2 means insert at index 2
//2nd param is delete item count = 0 means delete 0 elements
//3rd param is new item that you want to insert
//... is the spread syntax which will expand elements as one
suits.splice(2, 0 , ...newSuitsToInsert);
console.log(suits);
回答by Sanjay Kumar Singh
You should use splicefunction
您应该使用拼接功能
arr.splice(index, 0, item);will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert).
arr.splice(index, 0, item);将 item 插入到指定索引处的 arr (先删除 0 个项目,也就是说,它只是一个插入)。
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"]
suits.splice(2, 0, "somevalue");
console.log(suits);

