如何做“平推"在 JavaScript 中? [英] how to do a "flat push" in javascript?

查看:30
本文介绍了如何做“平推"在 JavaScript 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将源数组的所有单个元素推送到目标数组上,

I want to push all individual elements of a source array onto a target array,

target.push(source);

只将源的引用放在目标列表中.

puts just source's reference on the target list.

相反,我想做:

for (i = 0; i < source.length; i++) {
    target.push(source[i]);
}

javascript 中有没有一种方法可以更优雅地做到这一点,而无需明确编码重复循环?

Is there a way in javascript to do this more elegant, without explicitly coding a repetition loop?

当我在做的时候,正确的术语是什么?我不认为平推"是正确的.谷歌搜索没有产生任何结果,因为源和目标都是数组.

And while I'm at it, what is the correct term? I don't think that "flat push" is correct. Googling did not yield any results as source and target are both arrays.

推荐答案

您可以使用 concat 方法:

var num1 = [1, 2, 3];  
var num2 = [4, 5, 6];  
var num3 = [7, 8, 9];  

// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged  
var nums = num1.concat(num2, num3);

这篇关于如何做“平推"在 JavaScript 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆