admin管理员组文章数量:1353158
Let's say we have the following:
node[1].name = "apple";
node[1].color = "red";
node[2].name = "cherry";
node[2].color = "red";
node[3].name = "apple";
node[3].color = "green";
node[4].name = "orange";
node[4].color = "orange;
if I use jQuery.unique(node) I will get all the original nodes because they all have a different name OR color. What I want to do is only get the nodes with a unique name, which should return
node[1] (apple)
node[2] (cherry)
node[4] (orange)
It should not return 3 because it is the same fruit, even though we have green and red apples.
Let's say we have the following:
node[1].name = "apple";
node[1].color = "red";
node[2].name = "cherry";
node[2].color = "red";
node[3].name = "apple";
node[3].color = "green";
node[4].name = "orange";
node[4].color = "orange;
if I use jQuery.unique(node) I will get all the original nodes because they all have a different name OR color. What I want to do is only get the nodes with a unique name, which should return
node[1] (apple)
node[2] (cherry)
node[4] (orange)
It should not return 3 because it is the same fruit, even though we have green and red apples.
Share Improve this question edited Dec 9, 2013 at 21:21 David Hellsing 109k44 gold badges180 silver badges214 bronze badges asked Dec 9, 2013 at 20:44 user736893user736893 4- You'll have to iterate over your array, creating a new array with the unique matches. – Kevin B Commented Dec 9, 2013 at 20:54
-
I would instead use a different data structure where
node
is an object, and each key of the object is the fruit, each containing an array of colors. – Kevin B Commented Dec 9, 2013 at 20:55 - @KevinB Maybe the OP wants a certain ordering.... – David Hellsing Commented Dec 9, 2013 at 21:45
- Well they can each be objects, as a matter of fact that would be better, however the colors cannot be an array and there will be duplicates. – user736893 Commented Dec 9, 2013 at 21:54
3 Answers
Reset to default 7Use Array.filter
and a temporary array to store the dups:
function filterByName(arr) {
var f = []
return arr.filter(function(n) {
return f.indexOf(n.name) == -1 && f.push(n.name)
})
}
Demo: http://jsfiddle/mbest/D6aLV/6/
This approach (forked from @David's) should have better performance for large inputs (because object[]
is O(1)
).
function filter(arr, attribute) {
var out = [],
seen = {}
return arr.filter(function(n) {
return (seen[n[attribute]] == undefined)
&& (seen[n[attribute]] = 1);
})
}
console.log(filter(node, 'name'));
http://jsfiddle/LEBBB/1/
What about doing it like this?
var fruitNames = [];
$.each($.unique(fruits), function(i, fruit) {
if (fruitNames.indexOf(fruit.name) == -1) {
fruitNames.push(fruit.name);
$('#output').append('<div>' + fruit.name + '</div>');
}
});
Here is a working fiddle.
Obviously, instead of output.append I could just add the current fruit to uniqueFruit[] or something.
本文标签: javascriptGet unique objects from array based on single attributeStack Overflow
版权声明:本文标题:javascript - Get unique objects from array based on single attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743923753a2562574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论