我有一组来自 JSON 文件的键/值对。
$ $p.dependencies
@architect/architect : ^5.7.0
@architect/functions : ^3.0.4
assert : ^1.4.1
bcrypt : ^3.0.6
find-parent-dir : ^0.3.0
hashids : ^1.2.2
http-status-codes : ^1.3.2
lodash.get : ^4.4.2
mini-web-server : ^1.0.2
mini-webhook-server : ^1.0.4
mocha : ^5.2.0
opencorporates : ^3.0.0
stripe : ^6.23.1
uuid : ^3.3.2
whois-json : ^2.0.4
Run Code Online (Sandbox Code Playgroud)
看起来在 Powershell 中它们是noteProperty:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
@architect/architect NoteProperty string @architect/architect=^5.7.0
@architect/functions NoteProperty string @architect/functions=^3.0.4
assert NoteProperty string assert=^1.4.1
bcrypt NoteProperty string bcrypt=^3.0.6
find-parent-dir NoteProperty string find-parent-dir=^0.3.0
hashids NoteProperty string hashids=^1.2.2
http-status-codes NoteProperty string http-status-codes=^1.3.2
lodash.get NoteProperty string lodash.get=^4.4.2
mini-web-server NoteProperty string mini-web-server=^1.0.2
mini-webhook-server NoteProperty string mini-webhook-server=^1.0.4
mocha NoteProperty string mocha=^5.2.0
opencorporates NoteProperty string opencorporates=^3.0.0
stripe NoteProperty string stripe=^6.23.1
uuid NoteProperty string uuid=^3.3.2
whois-json NoteProperty string whois-json=^2.0.4
Run Code Online (Sandbox Code Playgroud)
我想将键为 的项目更改@architect/functions为与以下值不同的值^3.0.4(然后将数据保存回来)
下一部分似乎是从哈希表中选择正确的项目。我在用:
$p.dependencies | where $_.key -eq "@architect/functions"
Run Code Online (Sandbox Code Playgroud)
但是这不会返回任何结果。如何使用 key 选择项目@architect/functions?对于奖励积分,我如何更改其值!
编辑:
使用答案,如果有人觉得有用的话,这是我的最终脚本。
ls 'src/http' | foreach {
$packageJSONFile = "${PSItem}\package.json"
$packageJSON = cat $packageJSONFile | convertfrom-json
if ( $packageJSON.dependencies.'@architect/functions' ) {
$packageJSON.dependencies.'@architect/functions' = '^3.0.4'
}
$packageJSON | ConvertTo-Json -depth 100| set-content $packageJSONFile
}
Run Code Online (Sandbox Code Playgroud)
要获取名称中包含特殊字符的属性,请引用它:
$p.dependencies.'@architect/functions'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1412 次 |
| 最近记录: |