Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数组的多个项映射到单个对象/函数_Javascript_Arrays_Node.js - Fatal编程技术网

Javascript 将数组的多个项映射到单个对象/函数

Javascript 将数组的多个项映射到单个对象/函数,javascript,arrays,node.js,Javascript,Arrays,Node.js,考虑以下数组: ['123', '456', '789', '000', '111', '222', '333', '444', '555'] 现在,假设我想将每3项映射到一个函数 也就是说,123,456,789将被映射到一个函数(){…} 下一个000、111和222将映射到另一个函数(){…} 我之所以想这样做,是因为我需要执行对数据库的批处理请求,但我可以请求的最大ID量是每批25个 因此,我的目标是将每25项映射到一个函数(将执行批处理请求),然后使用async.parallel并行

考虑以下数组:

['123', '456', '789', '000', '111', '222', '333', '444', '555']
现在,假设我想将每3项映射到一个函数

也就是说,
123
456
789
将被映射到一个
函数(){…}

下一个
000
111
222
将映射到另一个
函数(){…}

我之所以想这样做,是因为我需要执行对数据库的批处理请求,但我可以请求的最大ID量是每批25个

因此,我的目标是将每25项映射到一个函数(将执行批处理请求),然后使用
async.parallel
并行执行每个函数

问题是我不能使用
map
async.map
,因为那样会映射数组中的每个项。我打算做的是将每个25个连续项映射到单个对象/函数

这可能吗


我正在寻找任何JavaScript或NodeJS解决方案。

与Node.js不完全相关,但这里有一个可能的解决方案:

var funcs = [a, b, c, ...], // the list of functions
    arr = [], // your data
    items = 25,
    mapOfFuncs = [];

for(var i = 0, len = arr.length, f; i < len; i++){
    f = funcs[Math.floor(i / items)];
    mapOfFuncs.push(f);
}
var data = ['123', '456', '789', '000', '111', '222', '333', '444', '555']
    , length = 3

// this function is called recursively until the whole initial array is processed (and emptied)
function splitTable() {

  var batch= data.splice(0, length)

    // process your batch here (map to the function you want)
    console.log('batch' + batch)


  if (data.length)
    splitTable()
}

// initial call of the function
splitTable()
var funcs=[a,b,c,…],//函数列表
arr=[],//您的数据
项目=25,
mapOfFuncs=[];
对于(变量i=0,len=arr.length,f;i
根据观察,每24个连续数字除以25和floored将产生一个相当于相应函数索引的特定数字

它将生成一个数组
mapOfFuncs
,该数组将具有与
arr
元素相对应的函数。类似于,对于
arr=['123','456','789','000','111','222','333','444','555']
items=3
,输出将是:
[a,a,a,b,b,b,c,c]
这里可以使用javascript函数,将原始数组中的每3(或25)个元素拆分一次

// this removes the first n elements out of the array and returns them
var batch= data.splice(0, length)
旁注:请注意,splice会修改原始阵列

// this removes the first n elements out of the array and returns them
var batch= data.splice(0, length)
以下是一个完整的解决方案:

var funcs = [a, b, c, ...], // the list of functions
    arr = [], // your data
    items = 25,
    mapOfFuncs = [];

for(var i = 0, len = arr.length, f; i < len; i++){
    f = funcs[Math.floor(i / items)];
    mapOfFuncs.push(f);
}
var data = ['123', '456', '789', '000', '111', '222', '333', '444', '555']
    , length = 3

// this function is called recursively until the whole initial array is processed (and emptied)
function splitTable() {

  var batch= data.splice(0, length)

    // process your batch here (map to the function you want)
    console.log('batch' + batch)


  if (data.length)
    splitTable()
}

// initial call of the function
splitTable()
更像这样

function groupBy(size, arr){
    for(var i=0, out=[]; i<data.length; i+=size)
        out.push( data.slice(i, i+size) )
    return out;
}

var data = ['123', '456', '789', '000', '111', '222', '333', '444', '555']; 

async.parallel(groupBy(3, data).map( part => processData.bind(null, part) ), callback);
//or
async.parallel(groupBy(3, data).map( part => ()=>processData(part) ), callback);
//or
async.map(groupBy(3, data), processData, callback);
功能分组依据(大小、arr){
对于(var i=0,out=[];i processData.bind(null,part)),回调);
//或
async.parallel(groupBy(3,data).map(part=>()=>processData(part)),回调);
//或
map(groupBy(3,数据),processData,回调);

对不起,这不是我的意思。我希望
arr
上的数据映射到
函数
对象,而不执行它。(此解决方案实际上是在执行我的函数,我还必须手动创建我的functon数组)@matiascero通过“映射到函数”,您的意思是作为参数传递给函数吗?不,我的意思是,就像您可以将
“abc”
映射到对象
{name:“abc”}
,我希望
“abc”
映射到
函数
对象。
[“abc”,“def”]->[函数,函数]
那么您需要这样的东西吗?array.mapplicple(arr,3,function(data){//data是一个由三个元素组成的数组//do your mapped function});映射函数是不同的函数?还是与数组中的函数相同。map?请注意:
splice
将修改原始数组。事实上,我在答案中添加了一个注意事项。