最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get unique objects from array based on single attribute - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 7

Use 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.

发布评论

评论列表(0)

  1. 暂无评论