Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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_Php - Fatal编程技术网

Javascript 如何添加数字并将结果放入数组

Javascript 如何添加数字并将结果放入数组,javascript,php,Javascript,Php,我使用了一个fro条件,用1添加了85,用1再次添加了结果; 即 我需要将所有85,86,87,88放在一个数组中 如何使用一个简单的循环将它们调用到一个数组中,您可以这样做 $array = array(); $n = 85; // the start number $limit = 88; // The max value to reach while($n <= $limit) { // increment by 1 and store into array $n++

我使用了一个fro条件,用
1
添加了
85
,用
1
再次添加了结果; 即

我需要将所有
85,86,87,88
放在一个数组中


如何使用一个简单的循环将它们调用到一个数组中

,您可以这样做

$array = array();
$n = 85; // the start number
$limit = 88; // The max value to reach
while($n <= $limit) {
    // increment by 1 and store into array
    $n++;
    $array[] = $n; 
}
$array=array();
$n=85;//起始号码
$limit=88;//要达到的最大值

而($n您可以在JavaScript中执行此操作

var arr=Array(3)//创建大小为3的数组
.fill(86)//用86填充
.map(函数(v,i){
返回v+i//迭代并更新值
});
文件。写入(arr);

简短回答 深入 标签
PHP
附在您的问题之后,因此我假设我可以使用PHP来形成我的答案

range()
函数将使您的生活更加轻松。它将为数组分配一系列数字、字母等,而无需循环

// Create the array
$numberArray = range(1, 100);

// Create an alphabetic array
$alphaArray = range('a', 'z');

var_dump($numberArray);

// Output
array(
    [0] => 1,
    [1] => 2,
    [2] => 3,
    [3] => 4,
    ...
    [99] => 100
)
在您的情况下,这将是:

$range = range(85, 88);

// Output
array(
    [0] => 85
    [1] => 86
    [2] => 87
    [3] => 88
)
我希望这能帮你一点忙

来源

这也是一种替代解决方案

$array = array();
$n = 85; //The start number
$limit = 88; //The max value to reach
while($n <= $limit) {
    //Increment by 1 and store into array
    $n++;
    array_push($array,$n); 
}

echo '<pre>';
print_r($array);

希望这将是有用的

var_initArray=[85];
函数\u myArray(添加\u次,数组){

对于(var i=0;iYou想要它在javascript中吗?不,这不起作用,只有最后一个值被添加到数组中,我需要所有85,86,87,88都在一个数组中,这里只有88个。您能接受我的答案吗?)@sivasanker
$range = range(85, 88);
// Create the array
$numberArray = range(1, 100);

// Create an alphabetic array
$alphaArray = range('a', 'z');

var_dump($numberArray);

// Output
array(
    [0] => 1,
    [1] => 2,
    [2] => 3,
    [3] => 4,
    ...
    [99] => 100
)
$range = range(85, 88);

// Output
array(
    [0] => 85
    [1] => 86
    [2] => 87
    [3] => 88
)
$array = array();
$n = 85; //The start number
$limit = 88; //The max value to reach
while($n <= $limit) {
    //Increment by 1 and store into array
    $n++;
    array_push($array,$n); 
}

echo '<pre>';
print_r($array);
Array
(
 [0] => 86
 [1] => 87
 [2] => 88
 [3] => 89
)
 var _initArray = [85];
 function _myArray(add_times,array){
 for(var i =0;i<=add_times;i++){
    //will give index of last element
    //_initArray[_initArray.length-1] will return last element
    var _getElem = _initArray[_initArray.length-1];
    //Add 1 with last element. 
    var _newElem = _getElem+1;
    _initArray.push(_newElem);
 }
 console.log(_initArray);
}
_myArray(5,_initArray);