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
[['A',40,true],['B',20,false].... In the current form, it's an array of four booleans.