Moo*_*oon 1476 javascript arrays
我需要在数组的开头添加或添加元素.
例如,如果我的数组如下所示:
[23, 45, 12, 67]
Run Code Online (Sandbox Code Playgroud)
我的AJAX调用的响应是34
,我希望更新的数组如下所示:
[34, 23, 45, 12, 67]
Run Code Online (Sandbox Code Playgroud)
目前我打算这样做:
var newArray = [];
newArray.push(response);
for (var i = 0; i < theArray.length; i++) {
newArray.push(theArray[i]);
}
theArray = newArray;
delete newArray;
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?Javascript是否有任何内置功能可以做到这一点?
我的方法的复杂性是O(n)
,看到更好的实现真的很有趣.
mea*_*gar 2680
使用unshift
.它就像是push
,除了它将元素添加到数组的开头而不是结尾.
unshift
/ push
- 在数组的开头/结尾添加一个元素shift
/ pop
- 删除并返回数组的第一个/最后一个元素一个简单的图表......
unshift -> array <- push
shift <- array -> pop
Run Code Online (Sandbox Code Playgroud)
和图表:
add remove start end
push X X
pop X X
unshift X X
shift X X
Run Code Online (Sandbox Code Playgroud)
查看MDN阵列文档.实际上,每个能够从数组中推送/弹出元素的语言都具有取消/移位(有时称为push_front
/ pop_front
)元素的能力,您自己永远不必实现这些元素.
Mak*_*sym 1358
var a = [23, 45, 12, 67];
a.unshift(34);
console.log(a); // [34, 23, 45, 12, 67]
Run Code Online (Sandbox Code Playgroud)
Abd*_*UMI 206
...
:DEMO
var arr = [23, 45, 12, 67];
arr = [34, ...arr]; // RESULT : [34,23, 45, 12, 67]
console.log(arr)
Run Code Online (Sandbox Code Playgroud)
zan*_*ngw 69
另一种方法是通过 concat
var arr = [1, 2, 3, 4, 5, 6, 7];
console.log([0].concat(arr));
Run Code Online (Sandbox Code Playgroud)
concat
和之间的区别unshift
是concat
返回一个新数组.他们之间的表现可以在这里找到.
function fn_unshift() {
arr.unshift(0);
return arr;
}
function fn_concat_init() {
return [0].concat(arr)
}
Run Code Online (Sandbox Code Playgroud)
这是测试结果
dre*_*mac 43
快速备忘单:
术语shift/unshift和push/pop可能有点令人困惑,至少对那些可能不熟悉C语言编程的人来说是这样.
如果您不熟悉术语,可以使用以下替代术语的快速翻译,这可能更容易记住:
* array_unshift() - (aka Prepend ;; InsertBefore ;; InsertAtBegin )
* array_shift() - (aka UnPrepend ;; RemoveBefore ;; RemoveFromBegin )
* array_push() - (aka Append ;; InsertAfter ;; InsertAtEnd )
* array_pop() - (aka UnAppend ;; RemoveAfter ;; RemoveFromEnd )
Run Code Online (Sandbox Code Playgroud)
小智 17
使用 ES6 解构:(避免改变原始数组)
const newArr = [item, ...oldArr]
ozi*_*x06 16
你有一个数组: var arr = [23, 45, 12, 67];
要将项目添加到开头,您要使用splice
:
var arr = [23, 45, 12, 67];
arr.splice(0, 0, 34)
console.log(arr);
Run Code Online (Sandbox Code Playgroud)
kab*_*hya 15
将新元素添加到数组中的备忘单
const list = [23, 45, 12, 67];
list.unshift(34);
console.log(list); // [34, 23, 45, 12, 67];
Run Code Online (Sandbox Code Playgroud)
2. Array#splice
const list = [23, 45, 12, 67];
list.splice(0, 0, 34);
console.log(list); // [34, 23, 45, 12, 67];
Run Code Online (Sandbox Code Playgroud)
3. ES6 传播...
const list = [23, 45, 12, 67];
const newList = [34, ...list];
console.log(newList); // [34, 23, 45, 12, 67];
Run Code Online (Sandbox Code Playgroud)
4. Array#concat
const list = [23, 45, 12, 67];
const newList = [32].concat(list);
console.log(newList); // [34, 23, 45, 12, 67];
Run Code Online (Sandbox Code Playgroud)
注意:在这些示例中的每一个中,您都可以通过提供更多要插入的项目来添加多个项目。
Har*_*hid 12
push()
在数组的末尾添加一个新元素.
pop()
从数组末尾删除一个元素.
unshift()
在数组的开头添加一个新元素.
shift()
从数组的开头删除一个元素.
使用 theArray.unshift(response)
如果你在一个数组的开头需要不断插入一个元素,它是更快地使用push
发言,然后是到呼叫reverse
,而不是调用unshift
所有的时间。
基准测试: http : //jsben.ch/kLIYf
var testdata = new Array();
testdata = [23, 45, 12, 67];
testdata = [34, ...testdata];
console.log(testdata)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)
使用splice
我们在开始处向数组插入一个元素:
arrName.splice( 0, 0, 'newName1' );
Run Code Online (Sandbox Code Playgroud)
如果要将数组中的元素推入数组的开头,请使用<func>.apply(<this>, <Array of args>)
:
const arr = [1, 2];
arr.unshift.apply(arr, [3, 4]);
console.log(arr); // [3, 4, 1, 2]
Run Code Online (Sandbox Code Playgroud)
实际上,所有的unshift
/ push
和shift
/ pop
发生变异的来源阵列。
的unshift
/ push
将项目添加到从开始/结束和存在阵列shift
/ pop
从数组的开始/结束删除项目。
但是,有一种方法可以不加突变地将项目添加到数组中。结果是一个新数组,要添加到数组末尾,请使用以下代码:
const originArray = ['one', 'two', 'three'];
const newItem = 4;
const newArray = originArray.concat(newItem);
Run Code Online (Sandbox Code Playgroud)
要添加到原始数组的开头,请使用以下代码:
const originArray = ['one', 'two', 'three'];
const newItem = 0;
const newArray = (originArray.reverse().concat(newItem)).reverse();
Run Code Online (Sandbox Code Playgroud)
通过上述方法,您无需更改即可添加到数组的开头/结尾。
小智 7
var array = [23, 45, 12, 67];
array.unshift(11);
alert(array);
Run Code Online (Sandbox Code Playgroud)