在数组中的测序对象上遇到麻烦

发布于 2025-01-20 14:48:10 字数 2865 浏览 0 评论 0原文

我有一系列从数据库获取的对象; 数组看起来如下:

[
 {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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2025-01-27 14:48:10
`let objArr = [
    { name: "conrad", sequence: 1 },
    { name: "sam", sequence: 1 },
    { name: "ron", sequence: 1 },
    { name: "dan", sequence: 2 },
    { name: "neil", sequence: 6},
];

let addObj = { name: "lily", sequence: 1 };
let nextseq = parseInt(addObj.sequence);

for (let i = 0; i < objArr.length; i++) {
    nextseq = getnextSeq(nextseq);
    console.log("nextseq",nextseq);
    objArr[i].sequence = nextseq;
    nextseq++;
}
objArr.push(addObj)
console.log(objArr);

function getnextSeq(nextSeq) {
    let duplicateArr = objArr.filter((e) => e.sequence == nextSeq);
    if (duplicateArr.length > 1) {
        nextSeq++
        duplicateArr.pop();
        getnextSeq(nextSeq)
    }
    return nextSeq
}`
`let objArr = [
    { name: "conrad", sequence: 1 },
    { name: "sam", sequence: 1 },
    { name: "ron", sequence: 1 },
    { name: "dan", sequence: 2 },
    { name: "neil", sequence: 6},
];

let addObj = { name: "lily", sequence: 1 };
let nextseq = parseInt(addObj.sequence);

for (let i = 0; i < objArr.length; i++) {
    nextseq = getnextSeq(nextseq);
    console.log("nextseq",nextseq);
    objArr[i].sequence = nextseq;
    nextseq++;
}
objArr.push(addObj)
console.log(objArr);

function getnextSeq(nextSeq) {
    let duplicateArr = objArr.filter((e) => e.sequence == nextSeq);
    if (duplicateArr.length > 1) {
        nextSeq++
        duplicateArr.pop();
        getnextSeq(nextSeq)
    }
    return nextSeq
}`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文