You need to enable JavaScript to run this app. PowerShell如何将其他数组元素作为成员添加到目标数组对应行
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何将其他数组元素作为成员添加到目标数组对应行

PowerShell 数组横向合并实现方案

首先确保3个数组长度一致,索引可一一对应,通过遍历索引为Array1的每个元素追加自定义属性即可实现横向扩展,两种实现方式如下:

方式1:直接修改原Array1

for ($index = 0; $index -lt $Array1.Count; $index++) {
    # 追加Value2属性,值为Array2同索引元素的Value字段
    $Array1[$index] | Add-Member -NotePropertyName 'Value2' -NotePropertyValue $Array2[$index].Value -Force
    # 追加Value3属性,值为Array3同索引元素的Value字段
    $Array1[$index] | Add-Member -NotePropertyName 'Value3' -NotePropertyValue $Array3[$index].Value -Force
}

执行后直接输出$Array1即可看到需求的结构,-Force参数可避免属性已存在时报错。

方式2:生成新数组不修改原数据

如果需要保留原数组数据,可直接生成新的目标数组:

$targetArray = for ($index = 0; $index -lt $Array1.Count; $index++) {
    [PSCustomObject]@{
        Date   = $Array1[$index].Date
        Value  = $Array1[$index].Value
        Value2 = $Array2[$index].Value
        Value3 = $Array3[$index].Value
    }
}

执行后$targetArray就是你需要的结果。

内容的提问来源于stack exchange,提问作者JW1990

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 05:24:02