删除本地存储中的重复数据

发布于 2025-01-11 00:09:12 字数 408 浏览 0 评论 0原文

我在从 localStorage 中删除重复数据时遇到困难,该平台在 Cordova-Phone Gap 上使用 JavaScript,如何删除 localStorage 上的重复数据?

例如,按 ID 删除重复项 :

[{ID=1, NAME:"JAMES"},
{ID=1, NAME:"JAMES"},
{ID=2, NAME:"HENRY"},
{ID=3, NAME:"JOHN"},
{ID=3, NAME:"JAMES"}]

期望:

[{ID=1, NAME:"JAMES"},
{ID=2, NAME:"HENRY"},
{ID=3, NAME:"JOHN"}]

I am facing difficulty in deleting duplicate data from localStorage, the platform use JavaScript on Cordova-Phone Gap, how can I delete duplicate data on localStorage ?

For example remove duplicate by ID :

[{ID=1, NAME:"JAMES"},
{ID=1, NAME:"JAMES"},
{ID=2, NAME:"HENRY"},
{ID=3, NAME:"JOHN"},
{ID=3, NAME:"JAMES"}]

Expect :

[{ID=1, NAME:"JAMES"},
{ID=2, NAME:"HENRY"},
{ID=3, NAME:"JOHN"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

拒绝两难 2025-01-18 00:09:12

在您的具体示例中,如果您希望删除具有相同数字 ID的任何对象,您可以使用 for-each 循环来传输仅当对象尚未发送到共享其 ID 的索引时,每个元素中存储的对象才会存储到空数组的元素索引

就您而言,由于您没有 ID:0,因此第一个无重复数组的元素将是未定义的(并且应该通过filtering删除)。

这是一个工作示例(我在对象中将 = 替换为 :)。这将仅保留每个 ID 的第一个对象

let data = [
{ID:1, NAME:"JAMES"},
{ID:1, NAME:"JAMES"},
{ID:2, NAME:"HENRY"},
{ID:3, NAME:"JOHN"},
{ID:3, NAME:"JAMES"}
];

const nonDuplicatedData = [];

data.forEach(x => {
if (!nonDuplicatedData[x.ID]) nonDuplicatedData[x.ID] = x;
});

const filteredData = nonDuplicatedData.filter(n => {return n != undefined});

console.log(filteredData);

如果您想要最后一个而不是给定 ID 的第一个对象,只需从 forEach 循环中删除 if (!nonDuplicatedData[x.ID]) 即可继续替换对象,留下该 ID 号的最终出现。

let data = [
{ID:1, NAME:"JAMES"},
{ID:1, NAME:"JAMES"},
{ID:2, NAME:"HENRY"},
{ID:3, NAME:"JOHN"},
{ID:3, NAME:"JAMES"}
];

const nonDuplicatedData = [];

data.forEach(x => {
nonDuplicatedData[x.ID] = x;
});

const filteredData = nonDuplicatedData.filter(n => {return n != undefined});

console.log(filteredData);

显然,必须检索您的 localStorage 版本,将 JSON.parse 转换为对象并分配给变量(此处为 data)。过滤后,您必须对其进行JSON.stringify,然后再将其放回localStorage。另外,这需要数字 ID,这不是通用的解决方案。

In your specific example, where you wish to remove any objects with the same numerical ID, you can use a for-each loop to transfer the object stored in each element to the element index of an empty array only if an object has not already been sent to the index that shares its ID

in your case, since you have no ID:0, the first element of the no-duplicate array will be undefined (and should be removed by filtering).

Here is a working example (I replaced = with : in your objects). This will keep only the first object of each ID

let data = [
{ID:1, NAME:"JAMES"},
{ID:1, NAME:"JAMES"},
{ID:2, NAME:"HENRY"},
{ID:3, NAME:"JOHN"},
{ID:3, NAME:"JAMES"}
];

const nonDuplicatedData = [];

data.forEach(x => {
if (!nonDuplicatedData[x.ID]) nonDuplicatedData[x.ID] = x;
});

const filteredData = nonDuplicatedData.filter(n => {return n != undefined});

console.log(filteredData);

If, instead of the first object for a given ID you want the last, simply remove if (!nonDuplicatedData[x.ID]) from the forEach loop to keep replacing the object, leaving the final occurence for that ID number.

let data = [
{ID:1, NAME:"JAMES"},
{ID:1, NAME:"JAMES"},
{ID:2, NAME:"HENRY"},
{ID:3, NAME:"JOHN"},
{ID:3, NAME:"JAMES"}
];

const nonDuplicatedData = [];

data.forEach(x => {
nonDuplicatedData[x.ID] = x;
});

const filteredData = nonDuplicatedData.filter(n => {return n != undefined});

console.log(filteredData);

Obviously, your localStorage version will have to be retrieved, JSON.parsed into an object and assigned to a variable (data here). After filtering, you'd have to JSON.stringify it before putting it back to localStorage. Also, this requires numeric IDs, it is not a general solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文