Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

javascript - 将多个元素推送到数组(push multiple elements to array)

I'm trying to push multiple elements as one array, but getting error(我试图将多个元素作为一个数组推送,但是会出错)

> a = [] [] > a.push.apply(null, [1,2]) TypeError: Array.prototype.push called on null or undefined I'm trying to do similar stuff that I'd do in ruby, I was thinking that apply is something like * .(我正在尝试做类似我在ruby中做的事情,我认为apply就像* 。) >> a = [] => [] >> a.push(*[1,2]) => [1, 2]   ask by evfwcqcg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can push multiple elements into an array in the following way(您可以通过以下方式将多个元素推送到数组中)

var a = []; a.push(1, 2, 3); console.log(a);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...