0

I have JavaScript dict object like

[2,1920][2,1080][2,700][3,1200][3,1000][3,800][4,1000][4,900][4,1920]

I need an array of max values, key-wise, e. g.:

[2,1920][3,1200][4,1920]

How can I achieve this?

Pavlo
  • 43,301
  • 14
  • 77
  • 113
ITGenius
  • 129
  • 2
  • 14

3 Answers3

2

Simply loop over it and compute the maxima. Since you gave only a pseudo notation of your data structure, I can only give you (pythonic) pseudo code:

maxima = new dict()
for each (key, value) in your_object:
    if not( key ispartof maxima ) or maxima.get(key) < value:
        maxima.set(key, value)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • +1 actually I thought OP might have something like `var dict = [ [2, 1920], [2, 1080], [2, 700], [3, 1200], [3, 1000], [3, 800], [4, 1000], [4, 900], [4, 1920] ]`. So I was trying to iteration within iteration. can you share me how to dictionary in js? – Praveen Dec 26 '13 at 12:28
  • @Praveen: For a dictionary (unordered key-value map) just use an object with its properties. For the result, an array of tuples is an inefficient data structure, but you can easily convert. – Bergi Dec 26 '13 at 12:48
0

Not the shortest way (2n complex), but it works:

var dataArr = [[2,1920],[2,1080],[2,700],[3,1200],[3,1000],[3,800],[4,1000],[4,900],[4,1920]];
dataArr.sort(function (a,b) {
    if (a[0] === b[0]) {
        return b[1] - a[1];
    }
});
var key = dataArr[0][0];
var currentKey;
var index = 1;
var arrLength = dataArr.length;

for (index; index < arrLength; index++) {
    currentKey = dataArr[index][0];
    if (key === currentKey) {
        delete dataArr[index];
    } else {
        key = currentKey;
    }
}
console.log(dataArr);
Alex
  • 11,115
  • 12
  • 51
  • 64
  • The algorithm will fail on this array: `[[2,1920],[3,1200],[2,700]]` – Pavlo Dec 26 '13 at 12:40
  • 1
    Your comparison function is invalid (and might fail differently in different sort algorithms)! Btw, sorting has `O(n log n)` complexity – Bergi Dec 26 '13 at 12:45
0

I think this is what you want.

var dict, maxes, keys, keyi, key;

// you can represent your data in a dictionary like this
dict = {2: [1920,1080,700], 3: [1200,1000,800], 4: [1000,900,1920]};

// this will get the max values for each key in the dicionary
maxes = {};
keys = Object.keys(dict);
for (keyi = 0; keyi < keys.length; keyi++) { 
    key = keys[keyi];
    maxes[key] = Math.max.apply(Math, dict[key]); 
}

In the end, the maxes will contain:

{2: 1920, 3: 1200, 4: 1920}
zord
  • 4,538
  • 2
  • 25
  • 30
  • I think you should provide a way to convert 2d array to object like that. – Pavlo Dec 26 '13 at 12:42
  • And what an ugly way to iterate over object keys. I would use either `Object.keys(dist).forEach()` or `for (key in dict) {}`. – Pavlo Dec 26 '13 at 12:44
  • `forEach()` needs a function, so it's an unnecessary level in the [scope chain](http://davidshariff.com/blog/javascript-scope-chain-and-closures). `for (key in dict)` is iterating over the inherited members as well, so it should be avoided, or used with `Object.hasOwnProperty` according to [Douglas Crockford](http://yuiblog.com/blog/2006/09/26/for-in-intrigue) – zord Dec 26 '13 at 13:02
  • You've created `dict` as object literal, it won't inherit anything. – Pavlo Dec 26 '13 at 13:31
  • Of course it does. It inherits from `Object`. If any libraries you use, put something in `Object.prototype`, your object literals will inherit that as well. Read the article I linked in my previous comment. – zord Dec 26 '13 at 14:08
  • My bad, it does inherit, but from `Object.prototype`, not `Object`, which is a function (try `Object.getPrototypeOf({}) === Object.prototype`). All those properties are not enumerable (try `for (var key in {}) console.log(key)`), so you are safe to avoid checking `Object.hasOwnProperty()`. – Pavlo Dec 26 '13 at 14:28