我在这里很迷茫。给定一个数字n,我必须返回一个用值null填充的n * n嵌套数组。 如果n是3:
[
[null, null, null],
[null, null, null],
[null, null, null]
]
我很迷茫。我有这样的事情:
function generateMatrix (n) {
let item = 'null';
let array1 = [];
let solution = [];
array1.push(item.repeat(n));
solution.push(array1.repeat(n));
return solution;
}
我知道这是不对的,不仅因为它不起作用,而且因为它没有意义,我不知道该怎么做。请记住,我非常非常初级,刚开始学习JS。
提前致谢。
答案 0 :(得分:4)
您可以使用Array.from
和Array.fill
方法执行此操作。
function matrix(n) {
return Array.from(Array(n), () => Array(n).fill(null))
}
console.log(matrix(3))
您可以使用相同的方法创建尺寸为rows x columns
的矩阵。
function matrix(rows, cols) {
return Array.from(Array(rows), () => Array(cols).fill(null))
}
console.log(matrix(2, 4))
console.log(matrix(3, 2))
答案 1 :(得分:2)
$MaxThread = 10
$RunspacePool = [runspacefactory]::CreateRunspacePool(
[System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
)
[void]$RunspacePool.SetMaxRunspaces($MaxThread)
$RunspacePool.Open()
$UserCredential = Get-Credential
Import-Module ActiveDirectory
$Users = Get-ADUser -filter * -Properties * -SearchBase "OU=Disabled User Accounts,DC=private,DC=private"
$Today = (Get-Date).ToShortDateString()
$OneMonthAgo = (Get-Date).AddMonths(-1).ToShortDateString()
[Collections.ArrayList]$results = @()
$OnPrem = $Users | Where-Object {$_.mDBUseDefaults -eq "True"}
$365 = $Users | Where-Object{$_.mDBUseDefaults -ne "True"}
Write-Host "Start Date: " $OneMonthAgo -ForegroundColor Green
Write-Host "Total Users OnPrem: " ($OnPrem.mail).Count -ForegroundColor Green
$OnPremScriptblock = {
Param (
[System.Management.Automation.PSCredential]$Credential,
[string]$emailAddress,
[string]$startTime,
[string]$userName,
[string]$loginName,
[string]$DisabledDate,
[string]$OfficeLocation
)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://aserversomewhere.local/PowerShell/ -Authentication Kerberos -Credential $Credential
Import-PSSession $Session -AllowClobber -DisableNameChecking -Verbose:$false | Out-Null
$MessageCount = Get-MessageTrackingLog -recipients $emailAddress -Start $startTime.ToString() -ResultSize unlimited
$Object = New-Object PSObject -Property @{
User = $userName
Login = $loginName
Email = $emailaddress
Type = "OnPrem"
DisabledDate = $DisabledDate
Location = $OfficeLocation
MessagesReceived = $MessageCount.Count.ToString()
}
$Object
}
$jobs = New-Object System.Collections.ArrayList
foreach ($user in $OnPrem){
$PowerShell = [PowerShell]::Create()
$null = $PowerShell.AddScript($OnPremScriptblock)
$null = $PowerShell.AddArgument($UserCredential)
$null = $PowerShell.AddArgument($user.mail)
$null = $PowerShell.AddArgument($OneMonthAgo)
$null = $PowerShell.AddArgument($user.name)
$null = $PowerShell.AddArgument($user.samaccountname)
$null = $PowerShell.AddArgument($user.modified)
$null = $PowerShell.AddArgument($user.Office)
$PowerShell.RunspacePool = $RunspacePool
[void]$jobs.Add((
[pscustomobject]@{
PowerShell = $PowerShell
Handle = $PowerShell.BeginInvoke()
}
))
}
While($jobs.handle.IsCompleted -eq $false){
Write-Host "." -NoNewline
Start-Sleep -Milliseconds 100
}
$return = $jobs | foreach{
$_.PowerShell.EndInvoke($_.Handle)
$_.PowerShell.Dispose()
}
$jobs.Clear()
$return
答案 2 :(得分:1)
这是使用 Array.from
功能和 fill
功能的替代选择。
Rewind
var rows = 3;
var cols = 3;
var matrix = Array.from({length: rows}, () => new Array(cols).fill(null))
console.log(JSON.stringify(matrix, null, 2));
创建数组
.as-console-wrapper { max-height: 100% !important; top: 0; }
可让您从:
- 类似数组的对象(具有
Array.from()
属性和索引元素的对象)或- 可迭代对象(可以获取其元素的对象,例如Map和Set)。