我正在寻找一个JavaScript数组插入方法,样式如下:
arr.insert(index, item)
最好是在jQuery中,但此时任何JavaScript实现都可以。
我正在寻找一个JavaScript数组插入方法,样式如下:
arr.insert(index, item)
最好是在jQuery中,但此时任何JavaScript实现都可以。
当前回答
为了实现正确的函数编程和链接目的,Array.protocol.insert()的发明是必不可少的。实际上,如果拼接返回的是变异数组,而不是一个完全没有意义的空数组,那么它可能是完美的。因此,这就是:
Array.prototype.insert=函数(i,…rest){此.拼接(i,0,…其余)返回这个}变量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=函数(i,…rest){返回this.slice(0,i).contat(rest,this.slict(i));}变量a=[3,4,8,9],b=插入件(2,5,6,7);console.log(JSON.stringify(a));console.log(JSON.stringify(b));
其他回答
任何对这个问题仍有疑问的人,都尝试过前面答案中的所有选项,但都没有得到。我分享我的解决方案,这是为了考虑到您不想显式地声明对象与数组的财产。
function isIdentical(left, right){
return JSON.stringify(left) === JSON.stringify(right);
}
function contains(array, obj){
let count = 0;
array.map((cur) => {
if(this.isIdentical(cur, obj))
count++;
});
return count > 0;
}
这是对引用数组进行迭代并将其与要检查的对象进行比较的组合,将两者转换为字符串,然后在匹配时进行迭代。然后你就可以数了。这是可以改进的,但这是我解决的问题。
这是我在一个应用程序中使用的一个工作函数。
这将检查项目是否存在:
let ifExist = (item, strings = [ '' ], position = 0) => {
// Output into an array with an empty string. Important just in case their isn't any item.
let output = [ '' ];
// Check to see if the item that will be positioned exist.
if (item) {
// Output should be equal to an array of strings.
output = strings;
// Use splice() in order to break the array.
// Use positional parameters to state where to put the item
// and 0 is to not replace an index. Item is the actual item we are placing at the prescribed position.
output.splice(position, 0, item);
}
// Empty string is so we do not concatenate with comma or anything else.
return output.join("");
};
然后我把它叫做下面。
ifExist("friends", [ ' ( ', ' )' ], 1)} // Output: ( friends )
ifExist("friends", [ ' - '], 1)} // Output: - friends
ifExist("friends", [ ':'], 0)} // Output: friends:
通过执行以下操作,可以实现Array.insert方法:
Array.prototype.insert = function ( index, ...items ) {
this.splice( index, 0, ...items );
};
然后你可以像这样使用它:
var arr = [ 'A', 'B', 'E' ];
arr.insert(2, 'C', 'D');
// => arr == [ 'A', 'B', 'C', 'D', 'E' ]
如果您想一次将多个元素插入到数组中,请查看以下堆栈溢出答案:用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和ARRAY OF OBJECT可重用方法
设arr=[0,1,2];让obj=〔{name:“abc”},{name::“xyz”};const addArrayItemAtIndex=(数组,索引,newItem)=>{return[…array.sslice(0,索引),newItem,…array.sSlice(索引)];}//对于阵列console.log(addArrayItemAtIndex(arr,2159));//对于对象阵列console.log(addArrayItemAtIndex(obj,0,{name:“AMOS”}));