简体   繁体   English

如何在另一个函数中的函数内部使用数组中的值?

[英]how do I use values from an array inside a function in another function?

I created a function in Javascript that convert decimal numbers to binary, and I used an array to store the binary numbers, and made the function returns the array, what I want is, how do I get the values of that array and use it outside the function or in another function? 我用Javascript创建了一个将十进制数转换为二进制数的函数,并使用一个数组存储二进制数,并使该函数返回该数组,我想要的是,如何获取该数组的值并在外部使用它该功能或其他功能?

thats the function I was talking about, and that array I want is called numArray. 多数民众赞成在我正在谈论的功能,而我想要的数组称为numArray。

  function decimalToBinary(Num) { let bits = ''; let rem = Num; for(let i = 7; i >= 0; i--) { let divisor = Math.pow(2, i); let bitValue = Math.floor(rem / divisor); bits = bits + bitValue; rem = rem % divisor; } let numArray = []; for(let i = 0; i < bits.length; i++){ let bit = bits.charAt(i); let binaryNums = parseInt(bit); numArray.push(binaryNums); } return numArray; } /* what I want to do is to use a specific value from inside that array and use inside a second function, and then use if-statement to get the result I want */ function second() { //if-statement if(numArray[2] === 1){ //do something }else{ //do something else } } 

var binary = [] ;
function decimalToBinary(Num) {
    // do some logic push the output in binary
return binary; 
}

function second(argument){
 //Here you can access the binary
}

PS:- This is anti pattern if you update your question i can tell some more way to do it. PS:-这是反模式,如果您更新您的问题,我可以告诉您更多方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个函数的函数中使用一个函数 - How do I use a function inside another function's function 我如何从另一个 function 中的 function 退出(返回) - how do I exit (return) from a function inside a another function 我如何从此Firebase集合查询功能中获取“ retArr”数组。 我想将数组值用于其他功能 - How do i get the “retArr” array out from this firebase collection query function. I want the array values to used for another function 如何在Express路由内使用另一个功能/多个功能来处理数据库中的数据? - How do I use another function/multiple functions inside of an Express route in order to manipulate data from a database? 如何确定已从数组调用了哪个值以在另一个函数中使用? - How do I determine which value has been called from an array, to use in another function? 我如何将 ajax function 值从一个页面发送到另一个页面 - How do i send ajax function values from a page to another 如何使用JavaScript中另一个文件中的变量和函数? - How do I use variables and function from another file in javascript? 如何从异步函数中获取数组的所有值 - How do I get all the values of an array from an async function Javascript - 如何在另一个 function 中返回通用 function? - Javascript - How do I return a generic function inside another function? 如何在另一个函数内部调用一个函数? - How do I call a function inside of another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM