一个更好的方式来拼接一个数组在JavaScript数组 [英] A better way to splice an array into an array in javascript
本文介绍了一个更好的方式来拼接一个数组在JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
难道还有比这更好的方式来拼接一个数组在JavaScript另一个数组
VAR字符串='theArray.splice('+开始+,+数字+,'+ newItemsArray.join(',')+');';
与eval(String);
解决方案
您可以使用<一个href=\"https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/Function/apply\">apply避免EVAL:
变参= [开始,编号] .concat(newItemsArray);
Array.prototype.splice.apply(theArray,参数);
的<一个href=\"https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/Function/apply\">apply功能用于调用另一个函数,具有一个给定的上下文和参数,作为数组提供,例如:
如果我们所说的:
VAR NUMS = [1,2,3,4];
Math.min.apply(数学,NUMS);
应用函数将执行:
Math.min(1,2,3,4);
Is there a better way than this to splice an array into another array in javascript
var string = 'theArray.splice('+start+', '+number+',"'+newItemsArray.join('","')+'");';
eval(string);
解决方案
You can use apply to avoid eval:
var args = [start, number].concat(newItemsArray);
Array.prototype.splice.apply(theArray, args);
The apply function is used to call another function, with a given context and arguments, provided as an array, for example:
If we call:
var nums = [1,2,3,4];
Math.min.apply(Math, nums);
The apply function will execute:
Math.min(1,2,3,4);
这篇关于一个更好的方式来拼接一个数组在JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文