这里我试图创建具有固定行数和非固定数量的列的mtrix,如下所述。
var matrix=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0,0 ]]
我正在尝试此代码
function matrix1(m, n) {
for ( m = 9;m>0;m--)
{
for (var n=m;n>0; n--)
{
return Array.from({
length: m
}, () => new Array(n).fill(0));
}
document.write("<br>");
}
};
var cols=9
var counter=9;
matrix(counter,cols);
我期待此代码的输出与
类似var matrix=[[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0],
[0, 0],
[0]]
我的输出为
var matrix=[[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
我的代码有什么问题?
答案 0 :(得分:1)
您可以使用native
和array.map
array.fill
&#13;
答案 1 :(得分:1)
只需对代码进行小修改
$trg_id = $this->request->param('id');
if ($trg_id) {
$post = $this->request->post();
if ($post) {
$objTrackingGroup = ORM::factory('Orm_trackingGroup', $trg_id);
if ($objTrackingGroup->loaded()) {
$objTrackingGroup->values($post)
->save();
$this->rest_output($data, 200);
} else {
$data = array(
'code' => 500,
'error' => 'Unknown error'
);
$this->rest_output($data, 500);
}
} else {
$data = array(
'code' => 404,
'error' => 'Data not found!'
);
$this->rest_output($data, 404);
}
}else {
$data = array(
'code' => 404,
'error' => 'Data not found'
);
$this->rest_output($data, 404);
}
这将有效
现场工作示例:https://jsbin.com/?html,js,output
希望这会有所帮助。谢谢!