[英]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? 并且我想实现一个自定义的min()和max()原型来获得最小和最大只有'是'真的'......任何技巧? 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.
我坚持认为,因为我在思考.map()以提取某个属性但是如何检查是否启用了由于true / false属性而提取。
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... 所以我应该调用像Array.minElement(MyArray,'valueA')来获得最小值,但看起来它不起作用....它应该返回min ...
what I did wrong here?... 我在这里做错了什么?......
Thanks at all Cheers Luigi 谢谢所有Cheers Luigi
If MyArray
looks like: 如果
MyArray
看起来像:
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: 我会使用
filter
和map
的组合来获取数组,然后应用Math
方法:
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: 最后我重新实现了minElement和maxElement,如下所示:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.