简体   繁体   中英

Javascript custom array prototype

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM