如何推送到数组的特定位置?

发布于 2024-12-24 22:50:25 字数 277 浏览 2 评论 0原文

我正在尝试有效地编写一条语句,将其推入数组的位置 1,并将该位置中的任何内容推入该位置,或者将其推回某个位置。

array = [4,5,9,6,2,5]

#push 0 to position 1

array = [4,0,5,9,6,2,5]

#push 123 to position 1

array = [4,123,0,5,9,6,2,5]

写这个的最好方法是什么? (可接受 JavaScript 或 CoffeeScript)

谢谢!

I'm trying to efficiently write a statement that pushes to position 1 of an array, and pushes whatever is in that position, or after it back a spot.

array = [4,5,9,6,2,5]

#push 0 to position 1

array = [4,0,5,9,6,2,5]

#push 123 to position 1

array = [4,123,0,5,9,6,2,5]

What is the best way to write this? (javascript or coffeescript acceptable)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-12-31 22:50:26
array = [4,5,9,6,2,5]

#push 0 to position 1
array.splice(1,0,0)

array = [4,0,5,9,6,2,5]

#push 123 to position 1
array.splice(1,0,123)

array = [4,123,0,5,9,6,2,5]
array = [4,5,9,6,2,5]

#push 0 to position 1
array.splice(1,0,0)

array = [4,0,5,9,6,2,5]

#push 123 to position 1
array.splice(1,0,123)

array = [4,123,0,5,9,6,2,5]
逆流 2024-12-31 22:50:26

splice() 函数是唯一一个原生数组函数,可让您将元素添加到数组的特定位置

我将获得您在问题中输入的一个数组来进行描述

splice(position, numberOfItemsToRemove, item)
  • position = 您要添加新项目的位置
  • numberOfItemsToRemove = 这表示要删除的项目数量。那是
    表示删除将按照新项目添加的位置开始。

Ex = 如果你想在 123 个结果中添加 1 个位置,则如下所示 ([4,123,0,5,9,6,2,5])
但如果您将 numberOfItemsToRemove 设置为 1,它将删除后面的第一个元素
123 如果你给 2 那么它会删除 123 之后的两个元素。

  • item = 您添加的新项目
function my_func(){
        var suits = [4,0,5,9,6,2,5]
        
        suits.splice(1 , 0 , 123);

        document.getElementById('demo').innerHTML = suits;
}
<p id="demo">4,0,5,9,6,2,5</p>
<button id="btn01" onclick="my_func()">Splice Array</button>

The splice() function is the only native array function that lets you add elements to the specific place of an array

I will get a one array that you entered in your question to describe

splice(position, numberOfItemsToRemove, item)
  • position = What is the position that you want to add new item
  • numberOfItemsToRemove = This indicate how many number of items will deleted. That's
    mean delete will start according to the position that new item add.

Ex = if you want to add 1 position to 123 result will be like this ([4,123,0,5,9,6,2,5])
but if you give numberOfItemsToRemove to 1 it will remove first element after the
123 if you give 2 then its delete two element after 123.

  • item = the new item that you add

function my_func(){
        var suits = [4,0,5,9,6,2,5]
        
        suits.splice(1 , 0 , 123);

        document.getElementById('demo').innerHTML = suits;
}
<p id="demo">4,0,5,9,6,2,5</p>
<button id="btn01" onclick="my_func()">Splice Array</button>

柒七 2024-12-31 22:50:26

你不能只是这样做:

myArray[idx] = foo;

你将需要一个具有适当大小的数组,但我不确定在没有请求的索引的列表上进行拼接的行为是什么。

Could you not just do:

myArray[idx] = foo;

You will need to have an array with the appropriate size, but I'm not sure what the behavior for splice on a list that doesn't have the requested index is either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文