在数组中的测序对象上遇到麻烦
我有一系列从数据库获取的对象; 数组看起来如下:
[
{name:"conrad",sequence:1},
{name:"sam",sequence:1},
{name:"ron",sequence:1},
{name:"dan",sequence:2},
{name:"neil",sequence:2},
]
当我通过fym或onclick中添加另一个对象时;示例对象:
{name:"lily",sequence:1}
所需的所需输出如下:
[
{name:"conrad",sequence:2},
{name:"sam",sequence:3},
{name:"ron",sequence:4},
{name:"dan",sequence:5},
{name:"neil",sequence:6},
{name:"lily",sequence:1}
]
由于重复序列,我无法获得正确的测序 我得到的如下:
[
{name:"conrad",sequence:2},
{name:"sam",sequence:2},
{name:"ron",sequence:2},
{name:"dan",sequence:3},
{name:"neil",sequence:3},
{name:"lily",sequence:1}
]
任何帮助都将不胜感激:
我的代码:
let findSeq = await model.find({ sequence: data.sequence, services: data.services, site_id: data.site_id });
if(findSeq.length > 0){
let allSeq = await model.find({ services: data.services, site_id: data.site_id }).sort({sequence : 1});
if(allSeq.length > 0){
for (let i = 0; i < allSeq.length; i++) {
let old_id = new mongo.ObjectID(allSeq[i]._id);
let newSq = parseInt(allSeq[i].sequence) + 1;
if(data.id || data._id){
if(allSeq[i].sequence >= data.sequence && data._id != old_id) {
allSeq[i].sequence = newSq;
delete allSeq[i].id;
delete allSeq[i]._id;
var updateSeq = await model.updateOne({ _id: old_id }, allSeq[i]);
if((i + 1) < allSeq.length){
if(newSq != allSeq[i+1].sequence && newSq < allSeq[i+1].sequence){
break;
}
}
}
} else {
if(allSeq[i].sequence >= data.sequence) {
allSeq[i].sequence = newSq;
delete allSeq[i].id;
delete allSeq[i]._id;
var updateSeq = await model.updateOne({ _id: old_id }, allSeq[i]);
console.log(updateSeq);
if((i + 1) < allSeq.length){
if(newSq != allSeq[i+1].sequence && newSq < allSeq[i+1].sequence){
break;
}
}
}
}
}
}
}
I have an Array of objects which I fetch from Database;
array looks like below:
[
{name:"conrad",sequence:1},
{name:"sam",sequence:1},
{name:"ron",sequence:1},
{name:"dan",sequence:2},
{name:"neil",sequence:2},
]
when I add another object in through form or onclick; Example object:
{name:"lily",sequence:1}
desired output required is as follows:
[
{name:"conrad",sequence:2},
{name:"sam",sequence:3},
{name:"ron",sequence:4},
{name:"dan",sequence:5},
{name:"neil",sequence:6},
{name:"lily",sequence:1}
]
due to duplicate sequence I am not able to get proper sequencing
I got as follows:
[
{name:"conrad",sequence:2},
{name:"sam",sequence:2},
{name:"ron",sequence:2},
{name:"dan",sequence:3},
{name:"neil",sequence:3},
{name:"lily",sequence:1}
]
any help would be appreciated
my code:
let findSeq = await model.find({ sequence: data.sequence, services: data.services, site_id: data.site_id });
if(findSeq.length > 0){
let allSeq = await model.find({ services: data.services, site_id: data.site_id }).sort({sequence : 1});
if(allSeq.length > 0){
for (let i = 0; i < allSeq.length; i++) {
let old_id = new mongo.ObjectID(allSeq[i]._id);
let newSq = parseInt(allSeq[i].sequence) + 1;
if(data.id || data._id){
if(allSeq[i].sequence >= data.sequence && data._id != old_id) {
allSeq[i].sequence = newSq;
delete allSeq[i].id;
delete allSeq[i]._id;
var updateSeq = await model.updateOne({ _id: old_id }, allSeq[i]);
if((i + 1) < allSeq.length){
if(newSq != allSeq[i+1].sequence && newSq < allSeq[i+1].sequence){
break;
}
}
}
} else {
if(allSeq[i].sequence >= data.sequence) {
allSeq[i].sequence = newSq;
delete allSeq[i].id;
delete allSeq[i]._id;
var updateSeq = await model.updateOne({ _id: old_id }, allSeq[i]);
console.log(updateSeq);
if((i + 1) < allSeq.length){
if(newSq != allSeq[i+1].sequence && newSq < allSeq[i+1].sequence){
break;
}
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)