如何使 JSON 数组唯一 [英] How to make a JSON array unique

查看:39
本文介绍了如何使 JSON 数组唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
数组唯一值
使用 jQuery 从 JSON 数组中获取唯一结果

我有一个这样的 JSON 字符串

<预><代码>[对象 { id="38",product="foo"},对象 { id="38",product="foo"},对象 { id="38",product="foo"},对象 { id="39",product="bar"},对象 { id="40",product="hello"},对象 { id="40",product="hello"}]

此 JSON 数组中有重复值..如何使此 JSON 数组像这样唯一

<预><代码>[对象 { id="38",product="foo"},对象 { id="39",product="bar"},对象 { id="40",product="hello"}]

.我正在寻找使用较少迭代的建议,Jquery $.inArray 在这种情况下不起作用.

欢迎使用任何第三方库的建议.

解决方案

检查以下 SO 问题中的解决方案:

使用 jQuery 从 JSON 数组中获取唯一结果

您必须遍历数组并创建一个包含唯一值的新数组.

Possible Duplicate:
Array unique values
Get unique results from JSON array using jQuery

Im having a JSON string like this

[
 Object { id="38",product="foo"},
 Object { id="38",product="foo"},
 Object { id="38",product="foo"},
 Object { id="39",product="bar"},
 Object { id="40",product="hello"},
 Object { id="40",product="hello"}

]

There are duplicate values in this JSON array..how can i make this JSON array unique like this

[
 Object { id="38",product="foo"},
 Object { id="39",product="bar"},
 Object { id="40",product="hello"}
]

.Im looking for a suggestion that uses less iterations, Jquery $.inArray is not working in this case.

Suggestion to use any third party libraries are welcome.

解决方案

Check the solution in the following SO question:

Get unique results from JSON array using jQuery

You'll have to iterate through your array and create a new array which contains unique values.

这篇关于如何使 JSON 数组唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆