0

I have for example this multidimensional array:

MyArray[('A', 40, true), ('B', 20, false), ('C', 55, true), ('D', 17, true)]

and I'd like to implement a custom min() and max() prototype to get min and max only of thoses are 'true'... any trick? I'm stuck at it since I was thinking around .map() to extract a certain property but how to check if enabled to extract due of true/false property.


Precisely, I did:

var MyArray = [];

MyArray.push( {
   name: 'A',
   valueA: 40,
   valueB: 36,
   enabled: true
});

MyArray.push( {
   name: 'B',
   valueA: 20,
   valueB: 18,
   enabled: false
});

MyArray.push( {
   name: 'C',
   valueA: 55,
   valueB: 75,
   enabled: true
});

.
.
.

that's why I am looking for the max and min of ones that are with state true, excluding the ones that are false...

I tried to implement:

Array.minElement = function(array, prop) {
    return Math.min.apply(Math,
                array.filter(function(arr) { return arr['enabled']; })
                     .map(function(arr) { return arr[prop]; })
           );
}

Array.maxElement = function(array, prop) {
    return Math.max.apply(Math,
                array.filter(function(arr) { return arr['enabled']; })
                     .map(function(arr) { return arr[prop]; })
           );
}

so I should call like Array.minElement(MyArray, 'valueA') to obtain the minimum but looks like it doesn't work....and it should return the min...

what I did wrong here?...

Thanks at all Cheers Luigi

3
  • 2
    Show us what you've tried Commented Nov 4, 2013 at 16:33
  • You probably mean [['A',40,true],['B',20,false].... In the current form, it's an array of four booleans. Commented Nov 4, 2013 at 16:33
  • Can you show us what you've tried, and your expected output? Commented Nov 4, 2013 at 16:34

3 Answers 3

1

If MyArray looks like:

var MyArray = [['A', 40, true], ['B', 20, false], ['C', 55, true], ['D', 17, true]]

I’d do a combination of filter and map to get the array, then apply a Math method:

Math.max.apply(Math, 
    MyArray.filter(function(arr) { return arr[2]; })
           .map(function(arr) { return arr[1]; })
); // => 55
Sign up to request clarification or add additional context in comments.

Comments

1

At the end I re-implemented minElement and maxElement like this:

Array.minElement = function(array, prop) {
    var lowest = Number.POSITIVE_INFINITY;
    for (var i=array.length-1; i>=0; i--) {
        if (parseFloat(array[i][prop]) < lowest) lowest = parseFloat(array[i][prop]);
    }
    return lowest;
}

Array.maxElement = function(array, prop) {
    var highest = Number.NEGATIVE_INFINITY;
    for (var i=array.length-1; i>=0; i--) {
        if (parseFloat(array[i][prop]) > highest) highest = parseFloat(array[i][prop]);
    }       
    return highest;
}

and now it works well

Comments

0

Arrays cannot really be extended but here's an example.

var createSpecialArray = (function () {

    function max() {
        return this.reduce(function (maxVal, item) {
            var val = item[1];

            return item[2]? 
                ((maxVal === null || maxVal < val)? val : maxVal) : 
                maxVal;
        }, null);
    }

    return function createSpecialArray() {
        var arr = Array.apply([], arguments);

        arr.max = max;

        return arr;
    };

})();

Then you can do something like:

var myArray = createSpecialArray(['A', 40, true], ['B', 20, false], ['C', 55, true], ['D', 17, true]);

console.log(myArray.max()); //55

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.