jQuery-在对象数组中查找不同的值 [英] jQuery - Finding Distinct Values in Object Array
本文介绍了jQuery-在对象数组中查找不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象数组,其中每个对象都有标题,描述,族等字段.如何执行jQuery操作,以唯一的族名来捕获此数组中的所有对象-类似于SQL的DISTINCT子句?
I've got an array of objects where each object has fields like title, description, family, etc. How can I perform a jQuery operation that grabs all objects in this array with a unique family name - similar to SQL's DISTINCT clause?
推荐答案
您可以这样做:
var array = [{
familyName: "one"},
{
familyName: "two"},
{
familyName: "one"},
{
familyName: "two"}];
var dupes = {};
var singles = [];
$.each(array, function(i, el) {
if (!dupes[el.familyName]) {
dupes[el.familyName] = true;
singles.push(el);
}
});
单数是仅包含DISTINCT对象的数组
Singles is an array with only DISTINCT objects
EDIT - i have blogged about this and given a more elaborate answer http://newcodeandroll.blogspot.it/2012/01/how-to-find-duplicates-in-array-in.html
这篇关于jQuery-在对象数组中查找不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文