Thậm chí nếu @gimenete công trình trả lời, cách tốt nhất để làm những gì bạn muốn là để chuyển tiếp các yếu tố danh sách như đối số cho RPUSH như vậy:
var redis = require('redis'),
client = redis.createClient();
var arr = [1,2,3];
client.rpush.apply(client, ['testlist'].concat(arr));
// ... or with a callback
client.rpush.apply(client, ['testlist'].concat(arr).concat(function(err, ok){
console.log(err, ok);
}));
Ưu: - một chỉ dẫn duy nhất sẽ transmited để Redis
Nhược điểm: - một góc hợp cụ thể: .apply
sẽ ném một RangeError: Maximum call stack size exceeded
nếu độ dài danh sách đối số truyền cho RPUSH là quá lớn (một chút ove r 100 000 mục cho v8).
Từ MDC:
The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function.
Bạn muốn lặp qua các phần tử mảng và chèn chúng riêng rẽ. Bạn có thể viết một vòng lặp 'for' đơn giản hoặc sử dụng' Array.prototype.forEach'. – fardjad