假设我正在尝试推送多个调用,例如:
var fields = [1,2];
fields.push(test("#test1", 1));
fields.push(test("#test2", 2));
fields.push(test("#test3", 3));
fields.push(newTest("#test5", 3, 'something'));
function test(a, b){
// something here
}
function newTest(a, b, c){
// something here
}
有没有一种有效的方法可以在一次调用中完成这一切?喜欢:
fields.push(test({"#test1": 1, "#test2": 2, "#test3": 3}), newTest(3, 4, "something"));
或
fields.push(test(["#test1": 1, "#test2": 2, "#test3": 3]), newTest(3, 4, "something"));
最佳答案
您正在寻找的是 Array.prototype.concat
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
fields = fields.concat(
test("#test1", 1),
test("#test2", 2),
test("#test3", 3),
newTest("#test5", 3, 'something')
);
如果该值是一个数组,则推送该数组的值而不是整个数组。其他值直接推送。
关于javascript - 在一次调用 JavaScript 中发送多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49132756/