我有2个2D数组,想将a [0]与b [0]合并
function placeCursorAtEndofTextArea() {
var ta = document.querySelector('#txtDescription');
ta.selectionStart = ta.selectionEnd = ta.value.length;
ta.focus();
}
任何人都可以显示如何吗?
答案 0 :(得分:0)
您可以使用spread syntax
Spread语法允许迭代器在0+ 参数是可以预期的。
var a=[["a","b","c"],["d","e","f"]];
var b=[["1","2","3"],["4","5","6"]];
//if you are trying to achieve all elements in single array then
var result = [...a[0], ...b[0]];
console.log(result);
//if you are trying to achieve single element from first array and all from other then
var result = [...a[0][0], ...b[0]];
console.log(result);
关于Spread syntax的好文章
答案 1 :(得分:0)
在 docs 中,Array.splice的语法为
array.splice(start [,deleteCount [,item1 [,item2 [,...]]]])
如您所见,您可以在数组中添加多个元素,例如arr.splice(0,0,4,5)
,您要将2个值(4和5)添加到数组中。使用b[0]
作为第三个参数,您将整个数组添加到特定索引处。要添加单个值,您需要散布数组的值。您可以使用spread syntax。这样arr.splice(0,0,...[1,2])
将成为arr.splice(0,0,1,2)
var a=[["a","b","c"],["d","e","f"]];
var b=[["1","2","3"],["4","5","6"]];
a[0].splice(1,2,...b[0])
console.log(a[0]);