[英]Filter dynamic Array Values
I need help with coding. 我需要编码方面的帮助。
I need to filter 14 values from an array. 我需要从数组中过滤14个值。 The values are created dynamically.
这些值是动态创建的。 I want to compare values to 20.0 I only need two of the values to be higher than 20.0
我想将值与20.0进行比较,我只需要其中两个值大于20.0
I put my hopes into filter
method as Switch did not work. 我把我的希望进入
filter
的方法开关没有工作。 Thank you in advance! 先感谢您!
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun /*, thisp*/) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
function isBigEnough(element, index, array) {
return (filtered >= 20.0);
}
var filtered = [
(vol1l * 100).toFixed(1),
(vol2l * 100).toFixed(1),
(vol3l * 100).toFixed(1),
(vol4l * 100).toFixed(1),
(vol5l * 100).toFixed(1),
(vol6l * 100).toFixed(1),
(vol7l * 100).toFixed(1),
(vol1r * 100).toFixed(1),
(vol2r * 100).toFixed(1),
(vol3r * 100).toFixed(1),
(vol4r * 100).toFixed(1),
(vol5r * 100).toFixed(1),
(vol6r * 100).toFixed(1),
(vol7r * 100).toFixed(1)
].filter(isBigEnough);
if (filtered) {
testText.textContent = "JA! Gerät"
} else {
testText.textContent = "NO! Nein"
}
Why not just map the variables, get the adjusted values and filter it. 为什么不只是映射变量,获取调整后的值并对其进行过滤。
const
factor100 = v => 100 * v,
isBigEnough = v => v >= 20;
var filtered = [vol1l, vol2l, vol3l, vol4l, vol5l, vol6l, vol7l, vol1r, vol2r, vol3r, vol4r, vol5r, vol6r, vol7r]
.map(factor100)
.filter(isBigEnough);
This proposal works with built in prototypes of Array
. 该建议适用于
Array
内置原型。
I suggest to use a better iterable data structure for using an array directly. 我建议使用更好的可迭代数据结构直接使用数组。
The function isBigEnough try to check filtered
with 20.0
. 函数isBigEnough尝试检查是否使用
20.0
filtered
。 This will allways return false
because filtered
is undefined
由于已
filtered
undefined
这将始终返回false
function isBigEnough(element, index, array) {
return (element >= 20.0);
}
function isBigEnough(element, index, array) { return (element >= 20.0); } var filtered = [1.0.toFixed(1), 2.0.toFixed(1), 20.0.toFixed(1), 21.0.toFixed(1)].filter(isBigEnough) console.log(filtered)
isBigEnough
in a other Funtion isBigEnough
var values = [1, 2, 10, 11, 20, 22].filter(biggerThan10) var moreValues = values.filter(biggerThan20) console.log(values, moreValues) function isBigEnough(value, compareValue) { return (value >= compareValue); } function biggerThan10(value) { return isBigEnough(value, 10) } function biggerThan20(value) { return isBigEnough(value, 20) }
var biggerThan10 = isBigEnough(10) var biggerThan20 = isBigEnough(20) var values = [1, 2, 10, 11, 20, 22].filter(biggerThan10) var moreValues = values.filter(biggerThan20) console.log(values, moreValues) function isBigEnough(value, compareValue) { return (value >= compareValue); } function isBigEnough(compareValue) { return function(value) { return compareValue <= value } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.