【问题标题】:Connecting elements in separate arrays连接不同数组中的元素
【发布时间】:2014-08-27 08:57:04
【问题描述】:

我希望将两个数组连接在一起,有点。 我有两个数组,并希望它们的元素是“组合”的。

所以,假设我有一台扫描仪。 它为 array1 中的每个元素提供了一个值(如 US$)。 如果每个元素的值小于或等于 array2 中的值,我希望它返回一些东西(比如字符串,“这个产品卖得很便宜!”)。

我只希望它将数组中的一项链接到相同位置的另一项

两个数组(上面简单提到过):

var array1 = [741, 451, 54188, 5847, 5418, 54944, 310, 541, 7451, 10211, 113, 9115, 62, 2841, 52482481, 24];
var array2 = [15, 418, 488, 130000, 8482, 55, 16, 14, 2546, 651, 4521, 11, 54, 659, 542, 1152];

因此,741 将链接到 15、451 到 418、54188 到 488,等等。

在它们被“链接”之后,我将能够查看 array1 中元素的值(按值,我的意思是我的扫描仪为每个元素找到的值)是否等于/小于该元素的值数组2。

例如,将 741 放入我的扫描仪会返回 16。 这不少于 15,所以不会返回任何东西。 - 但是,将 451 放入我的扫描仪会返回 417。这小于 418,所以它会返回“这个产品卖得便宜!”

我希望这是有道理的。

【问题讨论】:

  • @zerkms 我是 Javascript 新手,不太了解 .indexOf()。你愿意解释一下吗?
  • @user3810560 - 我建议您熟悉MDN - linked here,您可以在其中查找任何 javascript 方法或任何 javascript 对象类型以查看它具有哪些方法。 .indexOf() 非常简单且完整地描述了there。如果您学习了良好的参考资料,那么您应该能够自己找到更多信息的答案。

标签: javascript arrays


【解决方案1】:

我会创建一个对象并将其放入单个数组中。

var my Obj = {no1:741, no2:15, text:'some text'}

这将使您无需扫描仪即可构建阵列,因为它已经存储在“已扫描”状态

【讨论】:

    【解决方案2】:

    您可以使用 javascript 对象,其工作方式类似于字典(或 hashmap)

    var myObj = {};
    array1.forEach(function(item, i) {
        myObj[item] = array2[i];
    });
    alert(myObj[741]);
    >>> 15
    

    在这种排列中,数组 1 包含 keys,数组 2 包含字典的 values

    【讨论】:

      【解决方案3】:

      这是一个简单的解决方案,它只使用.indexOf() 在第一个数组中查找产品代码,然后使用该索引在第二个数组中查找比较价格:

      Product Number: <input id="testVal" type="text" value="741"><br>
      Test Price: <input id="priceVal" type="text" value=15><br>
      <button id="go">Lookup</button><br><br>
      <div id="result"></div>
      
      document.getElementById("go").addEventListener("click", function() {
          var prod = +document.getElementById("testVal").value;
          var price = +document.getElementById("priceVal").value;
          var result = lookupValue(prod, price);
          document.getElementById("result").innerHTML = result;
      });
      
      
      function lookupValue(prod, price) {
          var productCodes = [741, 451, 54188, 5847, 5418, 54944, 310, 541, 7451, 10211, 113, 9115, 62, 2841, 52482481, 24];
          var prices = [15, 418, 488, 130000, 8482, 55, 16, 14, 2546, 651, 4521, 11, 54, 659, 542, 1152];
      
          var index;
          if (!prod || !price) {
              return "Please Enter a Product Number and Price";
          }
          if ((index = productCodes.indexOf(prod)) >= 0) {
              if (index < prices.length && price < prices[index]) {
                  return "This product is selling for cheap!";
              } else {
                  return "This product is not a bargain.";
              }
          } else {
              return "Product not found for comparison";
          }
      }
      

      工作演示:http://jsfiddle.net/jfriend00/4S64t/.


      有了这么多产品代码,使用.indexOf() 在第一个数组中查找匹配项确实没有问题。但是,如果您有很多产品代码,那么将它们索引到一个对象中以便直接查找会快得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 2018-05-08
        • 2012-12-20
        • 1970-01-01
        • 2022-11-25
        相关资源
        最近更新 更多