数组拼接移除数组中的所有元素

Array splice removes all elements from array

var currentAge = 26;
var maxAge = 100;
var amountPerDay = 30.50;

var calculation = (((maxAge - currentAge) * 365) * amountPerDay);
console.log(calculation);
var stringCalc = calculation.toString().split('').splice(2, 0, "9");
console.log(stringCalc);

console.log(stringCalc) 显示一个空数组。这和toString方法有关系吗?

注意:我试图将字符串“9”添加到数组的中间,而不是从数组中删除任何内容。

var currentAge = 26;
var maxAge = 100;
var amountPerDay = 30.50;

var calculation = (((maxAge - currentAge) * 365) * amountPerDay);
console.log(calculation);
var stringCalc = calculation.toString().split('');
console.log(stringCalc);
stringCalc.splice(2, 0, "9");
console.log(stringCalc);

splice()returns取出的项(本例none),不是拼接后的数组

拼接不return数组只是将其添加到现有数组中:

var currentAge = 26;
var maxAge = 100;
var amountPerDay = 30.50;

var calculation = (((maxAge - currentAge) * 365) * amountPerDay);
var numberArray = calculation.toString().split('')

numberArray.splice(2, 0, "9");
console.log(numberArray);

你理解中缺失的link是splice的return值,也就是删除项

var currentAge = 26;
var maxAge = 100;
var amountPerDay = 30.50;

var calculation = (((maxAge - currentAge) * 365) * amountPerDay);
console.log(calculation);
var stringCalc = calculation.toString().split('');
console.log(stringCalc);
// ["8", "2", "3", "8", "0", "5"]
console.log(stringCalc.splice(2, 0, "foo"));
// [] because no items were deleted, return value = array of deleted items
console.log(stringCalc)
// ["8", "2", "foo", 3", "8", "0", "5"]