【问题标题】:how to put same value in same array element如何将相同的值放在相同的数组元素中
【发布时间】:2017-01-20 11:33:01
【问题描述】:

我在 js 中工作,我得到了一个数组的响应如下:

我有什么结构的数组:

 [ 
  { test_name: 'comp 1 test 1',
    program_id: 1,
  },
  { test_name: 'comp 1 test 2',
    program_id: 1,
  },
  { test_name: 'complete 2 test 1',

    program_id: 2,
   } 
]

但我想得到它:

 [ 
  [
  { test_name: 'comp 1 test 1',
    program_id: 1,
  },
  { test_name: 'comp 1 test 2',
    program_id: 1,
  }
  ],
  [
   { test_name: 'complete 2 test 1',

    program_id: 2,
   } 
  ]
]

我正在使用 Promises 从数据库中获取数据

 var req1 = new Promise(function(resolve, reject) { 
    db1.serialize(function() {

        var exer_names = [];
        var prog_status = 1;
        db1.each("SELECT  prog_name, prog_status,id  from programs Where prog_createdby = "+userId+" and prog_orgid = "+prog_orgid+" and prog_status = "+prog_status, function(err, row) {

        exer_names.push({id : row.id, prog_name: row.prog_name});

        },function(){

            resolve(exer_names);

        });
    });
});

var req2 = new Promise(function(resolve, reject) { 
    db1.serialize(function() {

        var test_names = [];

        db1.each("SELECT * from tests Where test_createdby = "+userId+" and org_id = "+prog_orgid+"", function(err, row) {

            test_names.push({test_name: row.test_name,test_alias: row.test_alias,test_createdby: row.test_createdby,sequences: row.sequences, duration: row.duration, program_id: row.prog_id, id: row.id });

        },function(){

             resolve(test_names);

        });

    });
});

  Promise.all([req1,req2]).then(function(programs, progerror) {
    programs[0].forEach(function(program){
    // what should be there??
    });

  }).catch(function(err) {

    console.log('Catch: ', err);

});

【问题讨论】:

  • 您是否尝试过使用 group by 子句?
  • 那没有好处。

标签: javascript arrays json node.js


【解决方案1】:

您可以根据program_id创建数组,然后将这些数组推送到父数组中

var parent = [];
var child = [];
var another_child = [];
parent.push(child);
parent.push(another_child);

【讨论】:

  • 我正在从数据库中获取数组
  • 要么你可以改变你的数据库 shema,要么你必须遍历响应数组才能得到想要的结果
  • 你需要在循环内的 program_id 上应用一些逻辑,等待
  • 而不是对整个响应应用循环,您应该尝试根据 program_id 将响应划分为多个数组。然后对这些数组应用循环以创建子 JSON 对象。
  • stackoverflow.com/questions/14696326/… 你可以从这里获得帮助
【解决方案2】:

您可以创建一个以 program_id 为索引的二维数组,并使用 2 个嵌套循环正确填充它

var input =  [
      {
        test_name: 'comp 1 test 1',
        program_id: 1,
      },
      {
        test_name: 'comp 1 test 2',
        program_id: 1,
      },
      {
        test_name: 'complete 2 test 1',
        program_id: 2,
      }
    ];
    output = input.map(x => x.program_id).filter((x,i,arr) => arr.indexOf(x) === i).map(x => [x]);
    output.forEach((x,i,arr) => {
      input.forEach(y => {
        if (y.program_id === x[0]) x.push(y);
      })
    });
    output = output.map(x => x.slice(1));
    console.log(output);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多