转换/操作数组的方法是什么?

时间:2017-07-09 18:26:03

标签: javascript arrays numbers

我在尝试将代码模块化为函数时遇到了问题。

我尝试使用两个独立的rng结果数组,这种方式要求我将索引的整个值转换为单个数字值, 然后将它们存储在一个数组中。我一直在使用它作为填充我的数组的方法,但更喜欢它是一个我可以在任何数组上调用的函数,并返回一个新的调整结果。:

var outcome1 = rng(); var outcome2 = rng();
function rng() {
Array.from({length: 2}, () => Math.floor(Math.random() * 9));
}

values = [];
// Intended Digitize function for Arrays
while(outcome1 > 0) {
  values[values.length] = outcome1 % 10;
  outcome1 = parseInt(outcome1 / 10);
  values.reverse();
}

如果需要,我需要能够替换其中一些数字,然后将单值数字数组连接成一个数字值(基于结果可能包含多少位数(在numberAdjust之后))。由于这些值可以从rng(预期)产生0,因此该函数需要能够查看结果中是否有值为0,如果是,则替换该值,同时还设置一个布尔值为< ll; ll作为后来不同调整的标志。

// numberAdjust function
// regex to remove 0 giving outcome new .length /unsure if needs own func
// outcome1.replace(/[0]/g); ??
(if outcome1.toString().length <= 1 && outcome2.toString().length <= 1) {
  outcome00 = true;
  outcome1.push(1);
  outcome2.push(1);
} else if (outcome1.toString().length <=1) {
  outcome0_ = true;
  outcome1.push(1);
} else if (outcome2.toString().length <=1) {
  outcome_0 = true;
  outcome2.push(1);
} else {
  outcome = true; }

一旦使用新的推送值(或不是)重新创建了数值,它就需要能够将调整后的结果转换回数组单位数值(数字化函数),因为任何数字值都可能需要被操纵再次(布尔的理由)。 一旦(/如果有)操作完成,数字化阵列需要转换回数字。

function numberize(...args) {
  args = Number(args.join(''));
  return args;
}

此numberize函数需要能够将数字化数组转换回数字,并且还需要能够接收多个数组,并将这两个数组连接成一个数字。

基本上我只需要能够自由地将1或2个数组转换并存储到单个数字值的数组中。根据长度添加/替换这些数组中的值(如果rng结果提供0),并且能够将数字化数组转换回整数。

我感谢任何建议,谢谢:)

0 个答案:

没有答案