2

I have an array of objects which looks like this:

var array = [
   {competitor: "X", fruit: "Cherry", size: 10},
   {competitor: "X", fruit: "Banana", size: 20},
   {competitor: "Y", fruit: "Cherry", size: 5},
   {competitor: "Y", fruit: "Banana", size: 25}
]

How do I get the smallest and unique fruit according to their size regardless of competitor with the result of:

[
   {competitor: "X", fruit: "Banana", size: 20},
   {competitor: "Y", fruit: "Cherry", size: 5}
]
1
  • How do I get unique and smallest objects in an array smallest by sorting then unique by filtering Commented Aug 23, 2017 at 20:34

3 Answers 3

6

You can use Array.reduce to create a hash based on fruit name and overwrite if the current item's size is smaller than the stored item's size:

const array = [
   {competitor: "X", fruit: "Cherry", size: 10},
   {competitor: "X", fruit: "Banana", size: 20},
   {competitor: "Y", fruit: "Cherry", size: 5},
   {competitor: "Y", fruit: "Banana", size: 25}
];
const result = array.reduce((p, c) => {
  if (!p[c.fruit] || c.size < p[c.fruit].size) {
    p[c.fruit] = c;
  }
  return p;
}, {});

console.log(result);

// as array
const resultArray = Object.keys(result).map(x => result[x]);

// as an array if Object.values() is available
const resultValues = Object.values(result);

console.log(resultArray)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! it's working. However, I have trouble with Object.values but solved with this stackoverflow.com/questions/38748445/….
Glad to help out! I suppose Object.values is new enough that it's not 100% safe to rely on but glad you found a solution to that :)
1
var array = [
   {competitor: "X", fruit: "Cherry", size: 10},
   {competitor: "X", fruit: "Banana", size: 20},
   {competitor: "Y", fruit: "Cherry", size: 5},
   {competitor: "Y", fruit: "Banana", size: 25}
];

var temp = {};
for (var i = 0; i < array.length; i++) {
    var fruit = array[i].fruit;
    var size = array[i].size;
    if (!temp[fruit] || size < temp[fruit].size) {
        temp[fruit] = array[i];
    }
}

var result = [];
for (var key in temp) {
    result.push(temp[key]);
}
console.log(result);

Comments

0

You could use a hash table and a single loop.

var array = [{ competitor: "X", fruit: "Cherry", size: 10 }, { competitor: "X", fruit: "Banana", size: 20 }, { competitor: "Y", fruit: "Cherry", size: 5 }, { competitor: "Y", fruit: "Banana", size: 25 }],
    hash = Object.create(null),
    result = [];
    
array.forEach(function (o) {
    if (!(o.fruit in hash)) {
        hash[o.fruit] = result.push(o) - 1;
        return;
    }
    if (o.size < result[hash[o.fruit]].size) {
        result[hash[o.fruit]] = o;
    }
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.