gpt4 book ai didi

javascript - Array.prototype.push.apply 意外行为

转载 作者:行者123 更新时间:2023-11-28 15:15:11 32 4
gpt4 key购买 nike

我正在尝试使用 Array.prototype.push.apply 合并两个列表。

c = Array.prototype.push.apply(a, b);

但是,当第二个数组为 [] 时,这不会合并数组。

例如,如果

a = ['x', 'y', 'z']
b = []

c 将是 3

为什么会发生这种情况?[] 不应该像任何数组一样对待吗?

最佳答案

只需使用Array.prototype.concat:

c = a.concat(b);

关于javascript - Array.prototype.push.apply 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34309099/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com