0
请参阅此代码为什么内部循环没有中断。 我只想在内部循环条件满足 的情况下推入新数组,并且应该中断内部循环的迭代,但外部循环应该照常进行迭代。如何打破节点js中的内部异步循环
这里是我的代码:
var rows = [ RowDataPacket {
id: 1,
title: 'this is jg case',
description: 'this is reply to jg case',
type: 2,
created: Thu Mar 03 2016 12:50:20 GMT+0530 (IST),
group_id: '1,8',
AOI: '1,8' },
RowDataPacket {
id: 2,
title: 'this is jg case',
description: 'this is another reply to jg case',
type: 1,
created: Thu Mar 03 2016 12:55:01 GMT+0530 (IST),
group_id: '3,9',
AOI: '1,8' },
RowDataPacket {
id: 3,
title: 'this is jg case',
description: 'this is again another reply to jg case',
type: 1,
created: Thu Mar 03 2016 12:58:01 GMT+0530 (IST),
group_id: '3,8',
AOI: '1,8' }
];
var filteredRows = [];
async.each(rows, function(row, callback1){
var eachRow = row.group_id.split(',').map(Number);
async.each(eachRow, function(group, callback2){
if(AOI.indexOf(group) !== -1){
filteredRows.push(row);
console.log(group,row);
var fakeErr = new Error();
fakeErr.break = true;
return callback2(fakeErr);
}
callback2();
}, function(err) {
if (err && err.break){
console.log("should break the inner loop and continue for the next outer loop")
callback1();
}
else
callback1(err);
});
}, function(err) {
if (err)
callback(err, null);
callback(null, filteredRows);
});
预期输出:
var filteredRows = [ RowDataPacket {
id: 1,
title: 'this is jg case',
description: 'this is reply to jg case',
type: 2,
created: Thu Mar 03 2016 12:50:20 GMT+0530 (IST),
group_id: '1,8',
AOI: '1,8' },
RowDataPacket {
id: 3,
title: 'this is jg case',
description: 'this is again another reply to jg case',
type: 1,
created: Thu Mar 03 2016 12:58:01 GMT+0530 (IST),
group_id: '3,8',
AOI: '1,8' }
];