在 JavaScript 中使用 splice 函数
use of splice function in JavaScript
拼接函数不能写在console.log里面??
我都试过了这就是我试过的-
const arr1 = ["a","b","c","d","e","f"];
console.log(arr1.splice(1,4,"HI"));
此代码的输出= ['a', 'HI', 'f' ]
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");`enter code here`
console.log(arr1);
此代码的输出 = ['b'、'c'、'd'、'e']`
有什么区别?
在第一个示例中,您正在记录 splice
(包含已删除元素的数组)的 return 值 。
在第二个示例中,您正在记录 变异数组 。
const arr1 = ["a", "b", "c", "d", "e", "f"];
const return_value = arr1.splice(1, 4, "HI");
console.log({
return_value,
arr1
});
日志显示 splice
函数 返回的内容 ,这是一个已删除项目的数组。您似乎想查看原始数组的 new 内容,这不是 splice
returns 的内容,因此您必须单独显示它。
arr1.splice(1,4,"HI")
console.log(arr1);
实例:
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");
console.log(arr1);
或者如果你真的想要单行,你可以滥用逗号运算符:
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)
实例:
const arr1 = ["a","b","c","d","e","f"];
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)
拼接函数不能写在console.log里面?? 我都试过了这就是我试过的-
const arr1 = ["a","b","c","d","e","f"];
console.log(arr1.splice(1,4,"HI"));
此代码的输出= ['a', 'HI', 'f' ]
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");`enter code here`
console.log(arr1);
此代码的输出 = ['b'、'c'、'd'、'e']`
有什么区别?
在第一个示例中,您正在记录 splice
(包含已删除元素的数组)的 return 值 。
在第二个示例中,您正在记录 变异数组 。
const arr1 = ["a", "b", "c", "d", "e", "f"];
const return_value = arr1.splice(1, 4, "HI");
console.log({
return_value,
arr1
});
日志显示 splice
函数 返回的内容 ,这是一个已删除项目的数组。您似乎想查看原始数组的 new 内容,这不是 splice
returns 的内容,因此您必须单独显示它。
arr1.splice(1,4,"HI")
console.log(arr1);
实例:
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");
console.log(arr1);
或者如果你真的想要单行,你可以滥用逗号运算符:
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)
实例:
const arr1 = ["a","b","c","d","e","f"];
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)