Javascript 如何在同一数组元素中放置相同的值

Javascript 如何在同一数组元素中放置相同的值,javascript,arrays,json,node.js,Javascript,Arrays,Json,Node.js,我在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

我在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,
   } 
  ]
]
但我想得到如下结果:

 [ 
  { 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,
   } 
  ]
]
我使用承诺从数据库中获取数据

 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);

});

您可以基于程序id创建数组,然后将这些数组推送到父数组中

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

您可以基于程序id创建数组,然后将这些数组推送到父数组中

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

您可以创建一个按程序id索引的二维数组,并用2个嵌套循环正确填充它

var输入=[
{
测试名称:“组件1测试1”,
程序id:1,
},
{
测试名称:“组件1测试2”,
程序id:1,
},
{
测试名称:“完成2个测试1”,
节目编号:2,
}
];
输出=输入.map(x=>x.program_id).filter((x,i,arr)=>arr.indexOf(x)==i).map(x=>[x]);
output.forEach((x,i,arr)=>{
input.forEach(y=>{
如果(y.program_id==x[0])x.push(y);
})
});
output=output.map(x=>x.slice(1));

控制台日志(输出)您可以创建一个按程序id索引的2D数组,并用2个嵌套循环正确填充它

var输入=[
{
测试名称:“组件1测试1”,
程序id:1,
},
{
测试名称:“组件1测试2”,
程序id:1,
},
{
测试名称:“完成2个测试1”,
节目编号:2,
}
];
输出=输入.map(x=>x.program_id).filter((x,i,arr)=>arr.indexOf(x)==i).map(x=>[x]);
output.forEach((x,i,arr)=>{
input.forEach(y=>{
如果(y.program_id==x[0])x.push(y);
})
});
output=output.map(x=>x.slice(1));

控制台日志(输出)我正在从数据库获取数组您可以更改db shema,或者您必须遍历响应数组才能获得所需的结果您需要在循环内的程序id上应用一点逻辑,而不是在整个响应上应用循环,您应该尝试根据程序id将响应划分为多个数组,然后在这些数组上应用循环来创建子JSON对象。为此,您可以从这里获得帮助我从数据库获取数组您可以更改db shema,或者您必须遍历响应数组才能获得所需的结果您需要在循环内的程序id上应用少量逻辑,而不是在整个响应上应用循环,您应该尝试根据程序id将响应划分为多个数组,然后在这些数组上应用循环来创建子JSON对象。因此,你可以从这里获得帮助。你是否尝试过使用“按条款分组”的方法?那是没有好处的。你是否尝试过使用“按条款分组”的方法?那是没有好处的。