【问题标题】:merging two arrays in nodejs在nodejs中合并两个数组
【发布时间】:2016-10-07 09:23:14
【问题描述】:

此函数将返回一个包含商店详细信息和图像的数组。

function listshops(callback)
    {   
     var array=[3,4];
      async.each(array,function(dat,callback){

         async.parallel([

             function(callback){
                c    function listshops(callback)
    {   
     var array=[3,4];/*shopId*/
      async.each(array,function(dat,callback){

         async.parallel([

             function(callback){
                client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
                callback(null,data1);

               });
            },       

             function (callback) {
                client.connection.query('select * from image where shopId=?',dat,function(err,data2){
                callback(null,data2);

               });
            }
         ],
         function(err,data)
         {
         console.log(data);

         });
     });

    }

上面代码的输出是::http://i.stack.imgur.com/laxf8.png

我需要将包含店铺信息和图片的数组合并为一个数组。即店铺信息与对应的图片。

[
    { shopId: 3, shopName: '1', address: 'abc', contactNumber: 1234 },
    { imageId: 1, shopId: 3, imageUrl: 'aaa' },
    { imageId: 2, shopId: 3, imageUrl: 'bbb' } 
]

【问题讨论】:

    标签: arrays node.js express


    【解决方案1】:

    您可以使用 reduce 来展平数组: [[1],[2]] --> [1,2]

    var array = [[1,2],[3]];
    array.reduce(function(prev, curr) {
      return prev.concat(curr);
    });
    

    在你的情况下:

      function listshops(callback)
        {   
         var array=[3,4];/*shopId*/
          async.each(array,function(dat,callback){
    
             async.parallel([
    
                 function(callback){
                    client.connection.query('select * from shop where shopId=?',dat,function(err,data1){
                    callback(null,data1);
    
                   });
                },       
    
                 function (callback) {
                    client.connection.query('select * from image where shopId=?',dat,function(err,data2){
                    callback(null,data2);
    
                   });
                }
             ],
             function(err,data)
             {
                var result = data.reduce(function(prev, curr) {
                  return prev.concat(curr);
                });
                console.log(result);
             });
         });
    
        }
    

    【讨论】:

    • 您编写的代码完美运行。但这里的问题是: [ { shopId: 3, shopName: '1', address: 'abc', contactNumber: 1234 }, { imageId: 1, shopId: 3, imageUrl: 'aaa' }, { imageId: 2, shopId: 3, imageUrl: 'bbb' } ] [ { shopId: 4, shopName: '2', 地址: 'bbb', contactNumber: 1234 }, { imageId: 3, shopId: 4, imageUrl: 'ccc' }, { imageId: 4, shopId: 4, imageUrl: 'ddd' } ] 如何从这个数组中获取仅具有 shopId=3 的数据。
    • 当我给出 result[0] 时,它会获取两个商店的详细信息。当我给出结果 [1] 时,它会获取图像 id(1,3)。
    • stackoverflow.com/questions/37695717/…: 请检查这个问题。
    猜你喜欢
    • 2021-08-27
    • 2021-01-24
    • 2010-09-08
    • 2010-12-31
    • 2013-08-26
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多