为什么我从这个简单的拼接命令获得此输出? [英] Why am I getting this output from this simple splice command?

查看:111
本文介绍了为什么我从这个简单的拼接命令获得此输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,理解为什么我得到这个输出。

I have a small issue with understanding why I'm getting this output.

var arr = ["a", "b", "c", "d", "e", "f"];
arr.splice(2,0,"1");
console.log(arr);  

var arr2 = ["a", "b", "c", "d", "e", "f"];
arr2 = arr2.splice(2,0,"2");
console.log(arr2);

输出为:

[ 'a', 'b', '1', 'c', 'd', 'e', 'f' ]
[]  

为什么第二行输出不是:

Why is the second line of output not:

[ 'a', 'b', '2', 'c', 'd', 'e', 'f' ]  

是分配问题还是什么?

推荐答案

阅读拼接方法。它返回


包含已删除元素的数组。如果只删除一个元素,则返回一个元素的数组。

An array containing the removed elements. If only one element is removed, an array of one element is returned.

这样做

arr2 = arr2.splice(2,0,"2");

你用空数组覆盖初始数组 arr spl $返回 []

you overwrite initial array arr with empty array [] returned by splice.

这篇关于为什么我从这个简单的拼接命令获得此输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆