首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅包含特定数组值的单个弹出窗口。javascript

仅包含特定数组值的单个弹出窗口。javascript
EN

Stack Overflow用户
提问于 2013-11-12 10:08:07
回答 2查看 76关注 0票数 0

您好,我正在尝试制作一个仅包含数组中大于/小于某个数字的值的弹出窗口。我该怎么做呢?

代码语言:javascript
复制
<script>
var moe = [3,3.14,4.3,8,9,19,23,24,46,54,87];
var noe = moe.indexOf(23);

function myFunction()
{
alert(noe);
}
function compare(){
for (var i=0;i<moe.length;i++){
if (moe[i]>10){
alert(moe[i]); 
}
}
}
</script>
EN

回答 2

Stack Overflow用户

发布于 2013-11-12 10:16:58

如下所示:

代码语言:javascript
复制
function compare(){
    var out = [];
    for (var i=0;i<moe.length;i++){
        if (moe[i]>10){
            out.push(moe[i]);
        }
    }
    alert(out.join()); 
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-12 10:30:51

让我们来看看这个问题:

给定值的数组:values = [3,3.14,4.3,8,9,19,23,24,46,54,87];.

  • We
  1. 希望根据大于10的值过滤结果。
  2. 最后在警告对话框中输出结果(接受字符串)。

第一步是计算出过滤,然后如何将结果转换为alert()函数的应用程序的字符串。

代码语言:javascript
复制
(function() {

  var i, len, values, value, results, string_value;

  values  = [3,3.14,4.3,8,9,19,23,24,46,54,87];
  results = []; // Empty array which we will build in order during the filter

  for (i = 0, len = values.length; i < len; i++) {
    value = values[i]; // Not needed; used for readability
    if (value > 10) {
      results.push(value); // Add this value to the results array
    }
  }

  // Now that we have a result lets convert that to a string
  string_value = results.join(", ");

  // And output the result with some string concatenation
  alert("Filtered results: [ " + string_value + " ]");

  // The use of string_value is optional, you could in-line
  // this into the alert line

})();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19919615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档