duandanxiu6965
2016-02-19 06:07
浏览 7

用javascript在php中的数组

I have code like this:

$data = array();
for(int $i = 0; $i < 10; $i ++)
   $data[$i][] = 5;

I don't know what's name of $data[$i][] = 5; in php and how to write this code in javascript?

or other hands, how can I write code above in javascript?

Thanks

P/s: I want to write that code on nodeJs

图片转代码服务由CSDN问答提供 功能建议

我的代码如下:

  $ data = array  (); 
for(int $ i = 0; $ i&lt; 10; $ i ++)
 $ data [$ i] [] = 5; 
   
 
  

我不知道php中 $ data [$ i] [] = 5; 的名称以及如何在javascript中编写此代码?

或者其他人,如何在javascript中编写上面的代码?

谢谢

P / s: 我想写代码 nodeJs

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongmi4734 2016-02-19 06:09
    已采纳

    You can try Array#push to add a element for array

    var data = [];
    for(var i = 0; i < 10; i ++)
       if(!data[i]) data[i] = [];
       data[i].push(5);
    
    点赞 评论
  • dongou5100 2016-02-19 06:12

    Array.push() is a function used to insert a value into an array. But you can also use the C like way data[i] = 'value'.

    data = [];
    for (var i = 0; i < 10; i++)
      data.push([5]);
    
    document.write(JSON.stringify(data))

    As an alternative use

    data = [];
    for(var i = 0; i < 10; i ++)
       data[i] = [5];
    
    document.write(JSON.stringify(data));

    </div>
    
    点赞 评论
  • dreamevil0002 2016-02-19 06:14

    Use array.push() to add an item to the end of the array.

    var sample = new Array();
    sample.push(new Object());
    

    To do this n times use a for loop.

    var n = 100;
    var sample = new Array();
    for (var i = 0; i < n; i++)
        sample.push(new Object());
    

    Note that you can also substitute new Array() with [] and new Object() with {} so it becomes:

    var n = 100;
    var sample = [];
    for (var i = 0; i < n; i++)
        sample.push({});
    
    点赞 评论
  • duanche2007 2016-02-19 06:30

    in this case, can understand $data[$i][] = 5; as below

    $data[$i] = array();
    array_push($data[$i],5);
    // ex: [ 0 => [0 => 5]]

    in Javascipt:

    var data = [];
    for (var i = 0; i < 10; i ++){
      data[i] = [];
      data[i].push(5);
    }

    </div>
    
    点赞 评论
  • doubo82706 2016-02-19 07:03

    JavaScript code:

    var data = [];
    for (i = 0; i < 10; i++) {
        data[i] = [];
        data[i][data[i].length] = 5;
    }
    
    点赞 评论

相关推荐 更多相似问题