我正在寻找一个Javascript数组插入方法,风格为:
arr.insert(index, item)
最好是在jQuery中,但任何Javascript实现都会在这一点上完成。
我正在寻找一个Javascript数组插入方法,风格为:
arr.insert(index, item)
最好是在jQuery中,但任何Javascript实现都会在这一点上完成。
你想要的是什么 splice
本机数组对象上的函数。
arr.splice(index, 0, item);
将插入 item
成 arr
在指定的索引处(删除 0
首先是项目,也就是说,它只是一个插入)。
在这个例子中,我们将创建一个数组并将一个元素添加到索引2中:
var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());
你可以实现 Array.insert
这样做的方法:
Array.prototype.insert = function ( index, item ) {
this.splice( index, 0, item );
};
然后你就可以使用它:
var arr = [ 'A', 'B', 'D', 'E' ];
arr.insert(2, 'C');
// => arr == [ 'A', 'B', 'C', 'D', 'E' ]
insert
方法/* Syntax:
array.insert(index, value1, value2, ..., valueN) */
Array.prototype.insert = function(index) {
this.splice.apply(this, [index, 0].concat(
Array.prototype.slice.call(arguments, 1)));
return this;
};
它可以插入多个元素(作为原生元素 splice
并支持链接:
["a", "b", "c", "d"].insert(2, "X", "Y", "Z").slice(1, 6);
// ["b", "X", "Y", "Z", "c"]
/* Syntax:
array.insert(index, value1, value2, ..., valueN) */
Array.prototype.insert = function(index) {
index = Math.min(index, this.length);
arguments.length > 1
&& this.splice.apply(this, [index, 0].concat([].pop.call(arguments)))
&& this.insert.apply(this, arguments);
return this;
};
它可以将参数中的数组与给定数组合并,并且还支持链接:
["a", "b", "c", "d"].insert(2, "V", ["W", "X", "Y"], "Z").join("-");
// "a-b-V-W-X-Y-Z-c-d"
除了拼接之外,您可以使用这种方法,它不会改变原始数组,但会创建一个带有添加项的新数组。你应该尽可能避免变异。我在这里使用ES6传播运营商。
const items = [1, 2, 3, 4, 5]
const insert = (arr, index, newItem) => [
// part of the array before the specified index
...arr.slice(0, index),
// inserted item
newItem,
// part of the array after the specified index
...arr.slice(index)
]
const result = insert(items, 1, 10)
console.log(result)
// [1, 10, 2, 3, 4, 5]
这可以用来添加多个项目,通过稍微调整函数来使用其余运算符来处理新项目,并将其传播到返回的结果中
const items = [1, 2, 3, 4, 5]
const insert = (arr, index, ...newItems) => [
// part of the array before the specified index
...arr.slice(0, index),
// inserted items
...newItems,
// part of the array after the specified index
...arr.slice(index)
]
const result = insert(items, 1, 10, 20)
console.log(result)
// [1, 10, 20, 2, 3, 4, 5]
如果要一次将多个元素插入到数组中,请查看此Stack Overflow答案: 在javascript中将数组拼接成数组的更好方法
这里还有一些函数来说明这两个例子:
function insertAt(array, index) {
var arrayToInsert = Array.prototype.splice.apply(arguments, [2]);
return insertArrayAt(array, index, arrayToInsert);
}
function insertArrayAt(array, index, arrayToInsert) {
Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert));
return array;
}
最后这里是一个jsFiddle所以你可以为自己看到它: http://jsfiddle.net/luisperezphd/Wc8aS/
这就是你如何使用这些功能:
// if you want to insert specific values whether constants or variables:
insertAt(arr, 1, "x", "y", "z");
// OR if you have an array:
var arrToInsert = ["x", "y", "z"];
insertArrayAt(arr, 1, arrToInsert);
对于正确的函数编程和链接目的,发明 Array.prototype.insert()
是必不可少的。实际上,如果它已经返回变异数组而不是完全没有意义的空数组,那么splice可能是完美的。所以这就是它
Array.prototype.insert = function(i,...rest){
this.splice(i,0,...rest)
return this
}
var a = [3,4,8,9];
document.write("<pre>" + JSON.stringify(a.insert(2,5,6,7)) + "</pre>");
好吧,上面跟着 Array.prototype.splice()
一个人改变原始数组,有些人可能会抱怨“你不应该修改不属于你的东西”,这也可能是正确的。所以对于公益事业,我想再给一个 Array.prototype.insert()
它不会改变原始数组。在这里;
Array.prototype.insert = function(i,...rest){
return this.slice(0,i).concat(rest,this.slice(i));
}
var a = [3,4,8,9],
b = a.insert(2,5,6,7);
console.log(JSON.stringify(a));
console.log(JSON.stringify(b));
我推荐使用纯净的 JavaScript的 在这种情况下,JavaScript中也没有插入方法,但我们有一个方法 内置数组 为你做的工作的方法,它被称为 拼接...
让我们看看是什么 拼接()...
splice()方法通过删除来更改数组的内容 现有元素和/或添加新元素。
好的,想象我们下面有这个数组:
const arr = [1, 2, 3, 4, 5];
我们可以删除 3
喜欢这个:
arr.splice(arr.indexOf(3), 1);
它将返回3,但如果我们现在检查arr,我们有:
[1, 2, 4, 5]
到目前为止,这么好,但我们如何使用splice向阵列添加新元素? 让我们回到3 ...
arr.splice(2, 0, 3);
让我们看看我们做了什么......
我们用 拼接 再次,但这次是第二个论点,我们通过了 0,意味着我们想删除没有项目,但同时,我们添加第三个参数,即3将在第二个索引处添加...
你应该知道,我们可以 删除 和 加 同时,例如现在我们可以这样做:
arr.splice(2, 2, 3);
哪个会删除 2项 在索引2处,然后添加 3 在索引2处,结果将是:
[1, 2, 3, 5];
这显示了拼接中的每个项目如何工作:
array.splice(start,deleteCount,item1,item2,item3 ......)
另一个可能的解决方案,使用 Array#reduce
。
var arr = ["apple", "orange", "raspberry"],
arr2 = [1, 2, 4];
function insert(arr, item, index) {
arr = arr.reduce(function(s, a, i) {
i == index ? s.push(item, a) : s.push(a);
return s;
}, []);
console.log(arr);
}
insert(arr, "banana", 1);
insert(arr2, 3, 2);
即使已经回答了这个问题,我还是将这个说明添加到另一种方法中。
我想放一个 已知数量 将项目分成数组,进入特定位置,因为它们来自“关联数组”(即一个对象),根据定义,它不能保证按排序顺序排列。我希望得到的数组是一个对象数组,但由于数组保证了它们的顺序,所以对象在数组中按特定的顺序排列。所以我这样做了。
首先是源对象,从PostgreSQL检索的JSONB字符串。我想让它按每个子对象中的“order”属性排序。
var jsonb_str = '{"one": {"abbr": "", "order": 3}, "two": {"abbr": "", "order": 4}, "three": {"abbr": "", "order": 5}, "initialize": {"abbr": "init", "order": 1}, "start": {"abbr": "", "order": 2}}';
var jsonb_obj = JSON.parse(jsonb_str);
由于对象中的节点数是已知的,因此我首先创建一个具有指定长度的数组:
var obj_length = Object.keys(jsonb_obj).length;
var sorted_array = new Array(obj_length);
然后迭代对象,将新创建的临时对象放入阵列中的所需位置,而不进行任何“排序”。
for (var key of Object.keys(jsonb_obj)) {
var tobj = {};
tobj[key] = jsonb_obj[key].abbr;
var position = jsonb_obj[key].order - 1;
sorted_array[position] = tobj;
}
console.dir(sorted_array);
我试过这个,工作正常!
var initialArr = ["India","China","Japan","USA"];
initialArr.splice(index, 0, item);
索引是您要插入或删除元素的位置。 0,即第二参数定义要从中删除的索引的元素数 item是要在数组中创建的新条目。它可以是一个或多个。
initialArr.splice(2, 0, "Nigeria");
initialArr.splice(2, 0, "Australia","UK");
在特定索引处附加单个元素
//Append at specific position(here at index 1)
arrName.splice(1, 0,'newName1');
//1: index number, 0: number of element to remove, newName1: new element
//Append at specific position (here at index 3)
arrName[3] = 'newName1';
在特定索引处附加多个元素
//Append from index number 1
arrName.splice(1, 0,'newElemenet1', 'newElemenet2', 'newElemenet3');
//1: index number from where append start,
//0: number of element to remove,
//newElemenet1,2,3: new elements