-
莫回无
基本上,因为元素类型arr1是子数组,但元素类型addArr只是整数。所以你只需要addArr像下面这样将元素包装在一个数组上就可以了。const arr1 = [[1,2,3], [1,2,2], [4,3]];const addArr = [[3,3,3]];const result = [].concat(arr1 , addArr );console.log(result);
-
郎朗坤
这就是Array.prototype.concat()的工作方式:const a = [1, []];
const b = [3, 4];
const c = a.concat(b);
// [1, [], 3, 4]与您的代码没有区别。Concat嵌套数组const arr1 = [[1,2,3], [1,2,2], [4,3]];const addArr = [[3,3,3]]; // Or also, use [3, 3, 3]...const result = arr1.concat(addArr); // ...but than ([addArr]) hereconsole.log(result);解构数组const arr1 = [[1,2,3], [1,2,2], [4,3]];const addArr = [[3,3,3]]; // Wrap into additional [] const result = [...arr1, ...addArr]; console.log(result)
-
犯罪嫌疑人X
发生原因:.concat 将每个数组的内容添加到新数组中。arr1 里面的内容是 [1,2,3], [1,2,2], [4,3]addArr 里面的内容是 3,3,3因此结果。怎么做:const arr1 = [[1,2,3], [1,2,2], [4,3]];const addArr = [3,3,3];const result = [...arr1, addArr];console.log(result);
-
缥缈止盈
因为concat() 将两个或多个数组合并在一起。合并与添加或推入不同。它不会将数组作为值添加到其他数组中,而是将值合并在一起。例子 :const array1 = ['a', 'b', 'c'];const array2 = ['d', 'e', 'f'];const array3 = array1.concat(array2);console.log(array3);// expected output: Array ["a", "b", "c", "d", "e", "f"]// Your guess is : Array ["a", "b", "c", ["d", "e", "f"] ]您可以通过使用解决您的问题push()const arr1 = [[1, 2, 3], [1, 2, 2], [4, 3]];const addArr = [3, 3, 3];arr1.push(addArr)console.log(arr1);或者通过在数组中添加值(不推荐):const arr1 = [[1, 2, 3], [1, 2, 2], [4, 3]];const addArr = [3, 3, 3];const result = arr1.concat([addArr])console.log(result);或者也许是经典时尚:const arr1 = [[1, 2, 3], [1, 2, 2], [4, 3]];const addArr = [3, 3, 3];arr1[arr1.length] = addArrconsole.log(arr1);