Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从一个数组到另一个数组的复合数_Javascript_Arrays - Fatal编程技术网

Javascript 从一个数组到另一个数组的复合数

Javascript 从一个数组到另一个数组的复合数,javascript,arrays,Javascript,Arrays,最后,我希望创建两个数组,将数据从一个数组处理到另一个数组 -- 我一直在尝试(大约3个周末)根据变量的长度创建数组,但没有成功;在本例中,我将其设置为155…此步骤现在已完成 我希望以后使用数组作为参考,因此我选择不操纵它自己 然后我想创建另一个数组,它从第一个数组中提取序列号,以应用基本上是复利的内容 视觉 阵列1(运行天数顺序): 1,2,3,4,5 数组2(复合数): 1,3,6,10,15 我正在努力让这个数学动作起作用。此外,我希望复合组件也是一个变量。这可以作为一个称为方差的变量输

最后,我希望创建两个数组,将数据从一个数组处理到另一个数组

--

我一直在尝试(大约3个周末)根据变量的长度创建数组,但没有成功;在本例中,我将其设置为155…此步骤现在已完成

我希望以后使用数组作为参考,因此我选择不操纵它自己

然后我想创建另一个数组,它从第一个数组中提取序列号,以应用基本上是复利的内容

视觉

阵列1(运行天数顺序): 1,2,3,4,5

数组2(复合数): 1,3,6,10,15

我正在努力让这个数学动作起作用。此外,我希望复合组件也是一个变量。这可以作为一个称为方差的变量输入到计算中

let dateDifference = 155;

// creates empty array
const savingLength = [];

// iterates through length of array
for(i = 0; i < dateDifference; i++)
{
  // creates maximum days ARRAY until end of saving term
  // adds array index as array value, +1 to create iteration of days in base10
  base10 = i+1;
  savingLength.push(base10); 

}

// creates a savingAmount array that is populated with data from savingLength array
const savingAmount = savingLength.map(function(savingLength){
  // does calculation on savingAmount element and returns it

  // desired CALC. compound interest

  return savingLength + mathBit();
});


function mathBit() {
  savingAmount.forEach(saving => {

    y = saving - 1;
    x = example.startAmount;
    saving = x + y;

  });
}
console.log(savingAmount);
console.log(savingLength);
let dateDifference=155;
//创建空数组
常数SavingleLength=[];
//遍历数组的长度
对于(i=0;i{
y=保存-1;
x=示例.startAmount;
保存=x+y;
});
}
控制台日志(savingAmount);
控制台日志(保存长度);

当前,
mathBit
没有返回任何内容,因此当您的
.map
函数执行
时,返回savingLength+mathBit(),它将不起作用

使用三角形数字序列来计算第二个数组可能更容易:
a(n)=(n*(n+1))/2

Array.from
允许您从头开始创建数组,而无需任何重新分配或变异。试着这样做:

const dateDifference=5;
const savingleLength=Array.from(
{length:dateDifference},
(u,i)=>i+1
);
常数savingAmount=savingLength.map(i=>(i*(i+1))/2);
控制台日志(保存长度);

控制台日志(savingAmount)
当前,
mathBit
没有返回任何内容,因此当
.map
函数执行
时,返回savingLength+mathBit(),它将不起作用

使用三角形数字序列来计算第二个数组可能更容易:
a(n)=(n*(n+1))/2

Array.from
允许您从头开始创建数组,而无需任何重新分配或变异。试着这样做:

const dateDifference=5;
const savingleLength=Array.from(
{length:dateDifference},
(u,i)=>i+1
);
常数savingAmount=savingLength.map(i=>(i*(i+1))/2);
控制台日志(保存长度);

控制台日志(savingAmount)
您需要
[1,2,3,…155]
阵列做什么?为什么不干脆
i=>i<155?i+1:未定义
?您需要
[1,2,3,…155]
数组做什么?为什么不干脆
i=>i<155?i+1:未定义
?感谢您的解决方案。它似乎已经勾选了所有的方框。虽然我仍然不知道为什么三角数列是一种东西。除了解决我的问题之外,它们的一般用途是什么?它们在数学中经常出现。谢谢你的解决方案。它似乎已经勾选了所有的方框。虽然我仍然不知道为什么三角数列是一种东西。除了解决我的问题之外,它们的一般用途是什么?它们在数学中经常出现。