如何删除除剩余一条记录之外的所有记录?

问题描述 投票:0回答:1

我使用 getIds 函数调用所有 ID,并在 clearSales 函数中使用它。clearSales 函数在测试结束时运行。当我调用 clearSales 时,所有记录都被删除。我不想删除所有记录,只保留一条记录。如何根据下面的函数删除除一条记录之外的所有记录?

const getIds= async () => {
    let res = await axios({
        method: 'get',
        url: '/v1/sales'
    })
    expect(res.status).toBe(200)
    const ids = [];
    res.data.salesId.forEach(item => {
        ids.push(item.id);
    });
    return ids
};

export const clearSales = async () => {
    const idList = await getIds()
    let res = await axios({
        method: 'post',
        url: '/v1/feed/bulk_update',
        data: { "feed_ids": idList, "operation": "delete" },
    })
    expect(res.status).toBe(200)
};
javascript node.js typescript axios
1个回答
0
投票

只需从

idList
中删除您想要保留的记录的ID即可。这是一个实现示例:

export const clearAllOtherSales = async (remainingRecordId) => {
    let idList = await getIds();
    let index = idList.findIndex(id => id === remainingRecordId);
    if (index > -1) idList.splice(index, 1);
    let res = await axios({
        method: 'post',
        url: '/v1/feed/bulk_update',
        data: { "feed_ids": idList, "operation": "delete" },
    });
    expect(res.status).toBe(200);
}

clearAllOtherSales(115)
清除除 id 115 之外的所有销售。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.