gpt4 book ai didi

javascript - 满足条件时从数组中提取值

转载 作者:行者123 更新时间:2023-11-28 19:00:37 28 4
gpt4 key购买 nike

我有一个简单的数组:

var c = [4,5,6,7,8,9,10,11,12];

我正在迭代数组以尝试返回满足条件的值:

$.each(c, function() {
// I need to pass each value from the array as the
// last argument in the function below
var p = get_shutter_price( width, height, c );
if ( p > 0 ) {
// Return the value from the array which allowed the condition to be met
console.log( c );
}
});

这不会按预期工作,因为整个数组都被传递到函数中。

如何从数组中返回满足条件的值?

例如,如果数组中的数字 8 返回的价格大于 0,则返回 8

最佳答案

如果您想要 c 中满足条件的所有值,只需 filter

var c = [4,5,6,7,8,9,10,11,12];

var conditionsMet = c.filter(function (value) {
return 0 < get_shutter_price(width, height, value);
});

conditionsMet[0] 则成为第一个满足条件的人。

关于javascript - 满足条件时从数组中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633790/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com