andy4thehuynh
andy4thehuynh

Reputation: 2172

Count occurrences in javascript object

Object {green: 3, red: 2, blue: 1, yellow: 1, mint: 3}

I want to loop through this object and return the keys with the most occurrences (by value) in an array. In this instance, it would be mint and green like ["mint", "green"].

So far, I got this

for (var i = 0; i < key(obj).length; i++) {
  ... // no idea
}

Any help or direction appreciated. Thank you!

Upvotes: 0

Views: 1741

Answers (5)

theadam
theadam

Reputation: 4051

EDIT: I thought a more consie solution would be in order. Here is one:

var bar = { green: 3, red: 2, blue: 1, yellow: 1, mint: 3 };

var findMaxProperties = function (foo) {
  var keys = Object.keys(foo);
  var max = keys.map(function (key) {
    return foo[key];
  }).reduce(function (previous, current) {
    return Math.max(previous, current);
  });
  return keys.filter(function (key) {
    return foo[key] == max;
  });
};

var result = findMaxProperties(bar);
$('#result').text(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="result"></div>

If I understand your question correctly (and if that is the case then you really need to edit it :)) then you want to look through an object and find its properties that have the highest numeric value. If that's the case, then something along those lines would do the trick.

var foo = { green: 3, red: 2, blue: 1, yellow: 1, mint: 3 };

var maxValue = Number.MIN_VALUE, maxValueProperties = [];
for (var key in foo) {
  if(foo[key] == maxValue) {
    maxValueProperties.push(key);
  }
  if(foo[key] > maxValue) {
    maxValue = foo[key];
    maxValueProperties = [];
    maxValueProperties.push(key);
  }  
}

$('#result').text(maxValueProperties);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="result">result</div>

Upvotes: -1

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

You need this:

var maxValue = -10000000;
for(var prop in obj) {
 if(!obj.hasOwnProperty(prop)) {
  continue;
 }
 if(obj[prop] > maxValue) {
  maxValue = obj[prop];
 }
}
// show maxValue

Note: you need obj.hasOwnProperty(prop) because object in js has some special properties, which you don't need in your loop.

Upvotes: 0

Ben
Ben

Reputation: 5361

var data = {green: 3, red: 2, blue: 1, yellow: 1, mint: 3}
var maxProperty = null, maxValue = -1;
for (var prop in data) {
  if (data.hasOwnProperty(prop)) {
    var value = data[prop]
    if (value > maxValue) {
      maxProperty = prop
      maxValue = value
    }
  }
}

Upvotes: 0

Andreas
Andreas

Reputation: 21881

var obj = {"green": 3, "red": 2, "blue": 1, "yellow": 1, "mint": 3},
    keys = Object.keys(obj),    // get all keys as array
    max = Math.max.apply(Math, keys.map(function(key) { return obj[key]; })),    // find the biggest number of occurrences
    result;
	
result = keys.filter(function(key) {    // find the relevant keys
	return obj[key] == max;
});

console.log(result);

Upvotes: 2

MarcJames
MarcJames

Reputation: 189

You could do something like:

var someObjects = {
  green: 3,
  red: 2,
  blue: 1,
  yellow: 1,
  mint: 3
};


function findOccurances(of, inn) {
  var array = [];
  for (var obj in inn) {
    if (someObject[obj] == of) {
      array.push(obj);
    }
  }
  return array;
}

Upvotes: 0

Related Questions